标签:sizeof 修改 others get reduce 其他 line eof start
Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Dhaka Division decided to keep up with new trends. Formerly all n cities of Dhaka were connected by n two-way roads in the ring, i.e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Dhaka introduced one-way traffic on all n roads, but it soon became clear that it‘s impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a blank line and an integer n (3 ≤ n ≤ 100) denoting the number of cities (and roads). Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) - road is directed from city ai to city bi, redirecting the traffic costs ci.
For each case of input you have to print the case number and the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
题意:给出n个点, n条边, n条边把n个点组成一个”环”, 但是,有些边方向不对, 导致某些点无法到达其他点,现在告诉你每条边修改方向的代价, 问把n个点组成一个真正的环,使得每个点都可以到达其他任何点的代价是多少。
数据不大,简单的搜索一遍即可,一道简单的dfs
#include <iostream> #include <cstring> using namespace std; const int inf = 0X3f3f3f3f; int map[110][110] , vis[110]; int sum , n; void dfs(int s , int t , int val , int step) { if(s == t && step == n) { sum = min(sum , val); return ; } for(int i = 1 ; i <= n ; i++) { if(vis[i] != 1) { if(map[t][i] == 0 && map[i][t] != 0) { vis[i] = 1; dfs(s , i , val + map[i][t] , step + 1); vis[i] = 0; } if(map[t][i] != 0) { vis[i] = 1; dfs(s , i , val , step + 1); vis[i] = 0; } } } } int main() { int t; cin >> t; int ans = 0; while(t--) { ans++; cin >> n; for(int i = 0 ; i <= n ; i++) { for(int j = 0 ; j <= n ; j++) { map[i][j] = 0; } } for(int i = 0 ; i < n ; i++) { int x , y , z; cin >> x >> y >> z; map[x][y] = z; } memset(vis , 0 , sizeof(vis)); sum = inf; dfs(1 , 1 , 0 , 0); cout << "Case " << ans << ": " << sum << endl; } return 0; }
lightoj 1049 - One Way Roads(dfs)
标签:sizeof 修改 others get reduce 其他 line eof start
原文地址:http://www.cnblogs.com/TnT2333333/p/6071716.html