LightOJ--1049--One Way Roads(dfs)(好题)
来源:互联网 发布:windows tftp 服务器 编辑:程序博客网 时间:2024/05/19 20:42
Submit Status
Description
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
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
Sample Output
Case 1: 1
Case 2: 2
Case 3: 39
Case 4: 0
Source
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;vector<int>G[200];int n,top,map[200][200],s[200];bool vis[200];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;s[top++]=v;dfs(v,sum+1);}}int main(){int t,k=1;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=0;i<=n;i++)G[i].clear();int u,v,val,ans1,ans2;memset(s,0,sizeof(s));memset(vis,0,sizeof(vis));memset(map,0,sizeof(map));for(int i=0;i<n;i++){scanf("%d%d%d",&u,&v,&val);G[u].push_back(v);G[v].push_back(u);map[u][v]=val;}ans1=ans2=top=0;s[top++]=1;dfs(1,1);//访问的过程中得到一条迹,从而找到每一条路 for(int i=1;i<top;i++){u=s[i-1];v=s[i];if(map[u][v]==0)//如果这条路没有修建,反向 ans1+=map[v][u];}if(map[s[top-1]][1]==0) ans1+=map[1][s[top-1]];for(int i=top-1;i>=1;i--)//两个方向都要判断 {u=s[i];v=s[i-1];if(map[u][v]==0)ans2+=map[v][u];}if(map[1][s[top-1]]==0) ans2+=map[s[top-1]][1];printf("Case %d: %d\n",k++,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
- Paid Roads(DFS)
- Paid Roads(DFS)
- lightoj Winter 1084 (dfs&&bfs求最少堆数) 好题
- LightOJ - 1009 Back to Underworld (vector&dfs)好题 判断两个阵营中最多人数
- Codeforces Round #375 (Div. 2) -- E. One-Way Reform(dfs求欧拉回路)
- poj 1724 ROADS(dfs)
- POJ 1724 ROADS (DFS)
- LightOJ 1012 (简单dfs)
- Codeforces723E - One-Way Reform(Euler回路)
- 单因素方差分析(One Way ANOVA)
- ZSTU 4213: One-Way Roads【图论】【欧拉回路】
- POJ 3411 Paid Roads(dfs)
- mysql导出与导入
- hadoop之hdfs参数配置详解
- 程序大牛的博客集锦
- Hadoop2.6.0中YARN底层状态机实现分析
- iOS 判断当前viewcontroller是push还是present的方式显示
- LightOJ--1049--One Way Roads(dfs)(好题)
- 第四课:ARM底层开发笔记之arm体系结构及异常处理
- 数格子,,简单bfs
- android socket长连接
- Set和List,HashSet与HasnMap,HashMap详解,HashSet详解
- C++ 继承
- php连接数库 和ca
- 等待多少秒后执行操作
- JS中判断null、undefined与NaN的方法