2017年湖南多校对抗赛第10场-COJ1930-roads
2017-05-24 17:25
295 查看
1930: roads
Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 35 Solved: 12Description
Once upon a time there was a strange kingdom, the kingdom had n cities which were connected by n directed roads and no isolated city.One day the king suddenly found that he can’t get to some cities from some cities.How amazing!The king is petty so he won’t build some new roads to improve this situation,but he has superpowers that he can change the direction of any road.To do this,he will gain a certain fatigue value for a certain road.The king didn’t want to be too tired.So he want to know what is the smallest amount of fatigue value he will gain on the redirecting of roads so that from every city people can get to any other?
Input
The first line contains integer n (3<=n<=100) - amount of cities (and roads) in the king. 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 it costs ci.
Output
Output single integer - the smallest amount of fatigue value the king will gain on the redirecting of roads so that from every city people can get to any other.
Sample Input
3
1 3 1
1 2 1
3 2 1
3
1 3 1
1 2 5
3 2 1
Sample Output
1
2
Hint
one or more test cases.
Source
2017年湖南多校对抗赛第10场
Author
xtu
题目大意:有n个点,n条边,边调转方向有一定的花费,求是的各点可达的最小花费。
解题思路:加边的时候可以加一条反向的负权边,从1开始dfs返回1,分别处理正权和负权,最后与较小者。
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include<cctype> #include<queue> using namespace std; const int MAXM = 500; const int MAXN = 500; int tot,head[MAXN]; int ans1,ans2; bool vis[MAXN]; struct Edge { int from,to; int cost,next; }e[MAXN]; void add(int u,int v,int c) { e[tot].from=u; e[tot].to=v; e[tot].cost=c; e[tot].next=head[u]; head[u]=tot++; } int dfs(int rt,int fa) { vis[rt]=true; for(int i=head[rt];i!=-1;i=e[i].next) { int to=e[i].to; int cost=e[i].cost; if(to==fa) continue; if(cost<0) ans1-=cost; else ans2+=cost; if(vis[to]) return min(ans1,ans2); else return dfs(to,rt); } } int main() { int n; while(cin>>n) { tot=0; memset(head,-1,sizeof(head[0])*(n+5)); int u,v,c; for(int i=1;i<=n;i++) { cin>>u>>v>>c; add(u,v,c); add(v,u,-c); } ans1=0; ans2=0; memset(vis,0,sizeof(vis[0])*(n+5)); cout<<dfs(1,0)<<endl; } }
相关文章推荐
- 湖南多校对抗赛(2015.03.28) I Inversion Sequence
- 湖南多校对抗赛(2015.03.28)
- 湖南多校对抗赛(2015.4.6) A
- 2015.3.22 湖南多校对抗赛(第二场)
- Heaps(Contest2080 - 湖南多校对抗赛(2015.05.10)(国防科大学校赛决赛-Semilive)+scu1616)
- Contest2079 - 湖南多校对抗赛(2015.05.03) Problem A: Twenty-four point
- 湖南多校对抗赛(2015.4.6)B
- CSU 1663 2015湖南多校对抗赛06.22
- 湖南多校对抗赛(2015.4.6) G
- 湖南多校对抗赛(2015.03.28)CSU1547~1556 题解
- Clock Pictures(kmp + Contest2075 - 湖南多校对抗赛(2015.04.26))
- 第十二场湖南多校对抗赛总结
- 湖南多校对抗赛(2015.4.6) E
- 湖南多校对抗赛(2015.03.15)9题题解 ABCEFGHJK
- 湖南多校对抗赛3.28 D Simple String
- Clock Pictures(kmp + Contest2075 - 湖南多校对抗赛(2015.04.26))
- 湖南多校对抗赛3.28 J - Jerry's trouble
- Contest2075 - 湖南多校对抗赛(2015.04.26)-C Catalan Square(csu1576)
- Contest2071 - 湖南多校对抗赛(2015.03.28)
- Contest2073 - 湖南多校对抗赛(2015.04.06)