http://codeforces.com/problemset/problem/24/A
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland 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 Berland 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?
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. 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 single integer — 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.
3 1 3 1 1 2 1 3 2 1
1
3 1 3 1 1 2 5 3 2 1
2
6 1 5 4 5 3 8 2 4 15 1 6 16 2 3 23 4 6 42
39
4 1 2 9 2 3 8 3 4 7 4 1 5
0
/**
CF 24A “环”,图的遍历
题目大意:给定n个点和n条有向边,所有的边把n个点构成了一个“环”,问如何改变其中一些边的方向使得从任意一点开始都可以遍历其他各点。
解题思路:对于每两个点之间都进行双向建边,给定的权值为w,未给定的权值为0,沿一个方向遍历“环”统计权值和m,答案为min(m,sum-m);
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int head[105],ip;
int sum,flag[105],n;
void init()
{
memset(flag,0,sizeof(flag));
memset(head,-1,sizeof(head));
ip=0;
sum=0;
}
struct note
{
int v,w,next;
}edge[405];
void addedge(int u,int v,int w)
{
edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}
void dfs(int u,int pre)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==pre||flag[v])continue;
sum+=edge[i].w;
flag[v]=1;
dfs(v,u);
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
int m=0;
for(int i=0;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
m+=w;
addedge(u,v,w);
addedge(v,u,0);
}
dfs(1,-1);
printf("%d\n",min(sum,m-sum));
}
return 0;
}