lightoj 1049 - One Way Roads 【DFS】
来源:互联网 发布:照片p图软件 编辑:程序博客网 时间:2024/05/29 15:41
题目链接:lightoj 1049 - One Way Roads
1049 - One Way Roads
PDF (English) Statistics Forum
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
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.
Output
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.
Sample Input
Output for Sample Input
4
3
1 3 1
1 2 1
3 2 1
3
1 3 1
1 2 5
3 2 1
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
4
1 2 9
2 3 8
3 4 7
4 1 5
Case 1: 1
Case 2: 2
Case 3: 39
Case 4: 0
题意:n个点,每个点的入度、出度确定为1。给出n条有向边和反向该边的代价。问你连通这n个点的最小代价。
思路:题目给出的图,去掉一条边一定是一棵树,那么我们直接遍历一次,找到1-n的路径,一定是唯一的。按照这条路径统计花费就可以了。
AC代码:
#include <cstdio>#include <cstring>#include <vector>#define CLR(a, b) memset(a, (b), sizeof(a))using namespace std;vector<int> G[110];bool vis[110];int n;int Map[110][110];int rec[110], top;void DFS(int u, int sum) { vis[u] = true; if(sum == n) { return ; } for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(vis[v]) continue; rec[top++] = v; DFS(v, sum + 1); }}int main(){ int t, kcase = 1; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 1; i <= n; i++) { G[i].clear(); vis[i] = false; } CLR(Map, 0); for(int i = 1; i <= n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G[u].push_back(v); G[v].push_back(u); Map[u][v] = w; } top = 0; rec[top++] = 1; DFS(1, 1); int ans1 = 0; for(int i = 1; i < top; i++) { int u = rec[i-1]; int v = rec[i]; if(Map[u][v] == 0) { ans1 += Map[v][u]; } } if(Map[rec[top-1]][1] == 0) ans1 += Map[1][rec[top-1]]; int ans2 = 0; for(int i = top-1; i >= 1; i--) { int u = rec[i]; int v = rec[i-1]; if(Map[u][v] == 0) { ans2 += Map[v][u]; } } if(Map[1][rec[top-1]] == 0) ans2 += Map[rec[top-1]][1]; printf("Case %d: %d\n", kcase++, min(ans1, ans2)); } return 0;}
- lightoj 1049 - One Way Roads 【DFS】
- LightOJ--1049--One Way Roads(dfs)(好题)
- LightOJ - 1049 One Way Roads
- LightOJ 1049 One Way Roads
- Light OJ 1049 - One Way Roads (暴力 or DFS)
- lightoj1049 - One Way Roads
- ZSTU 4213: One-Way Roads【图论】【欧拉回路】
- Lightoj 1049 DFS
- [dfs树]「CEOI 2017」One-Way Streets
- DFS单向复制的疑问/Is it possible to configure one-way replication with DFS Replication?
- lightOJ 1002:Country Roads
- LightOj 1002 - Country Roads
- 【LightOJ 1002】 Country Roads
- LightOJ 1002 Country Roads
- 1002 - Country Roads(lightoj)
- Codeforces Round #375 (Div. 2) -- E. One-Way Reform(dfs求欧拉回路)
- One-way Web Hacking
- one way单程杀机
- hive 配置参数说明
- TopCoder SRM687 div1 500
- 数字:
- Google hosts 更新
- 如果看了这篇文章你还不懂傅里叶变换,那就过来掐死我吧(二)
- lightoj 1049 - One Way Roads 【DFS】
- 垃圾收集器
- 小谈Java Enum的多态性
- Linux常用命令
- 一个差点被IoGetDeviceObjectPointer搞死的菜鸟
- SSM整合 错误收集之 listener
- Html5 离线应用缓存application Cache
- 记录与分享
- linux内核调试技巧之一 dump_stack