您的位置:首页 > 其它

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: 12

Description

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: