A. Ring road

来源:互联网 发布:mac 梦幻西游手游网页 编辑:程序博客网 时间:2024/06/09 23:29
A. Ring road
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

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 aibici (1?≤?ai,?bi?≤?n,?ai?≠?bi,?1?≤?ci?≤?100) — road is directed from city ai to city bi, redirecting the traffic costs ci.

Output

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.

Sample test(s)
input
3
1 3 1
1 2 1
3 2 1
output
1
input
3
1 3 1
1 2 5
3 2 1
output
2
input
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
output
39
input
4
1 2 9
2 3 8
3 4 7
4 1 5
output
0
很好的题目
/* ***********************************************
Author :
Created Time :2015/6/11 21:50:17
File Name :7.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int a[110][110];
vector
<int>v[maxn];
void init(){
for(int i=1;i<=102;i++)
for(int j=1;j<=102;j++)
a
[i][j]=INF;
}
int n;
int ans;
int bs(int t){
//cout<<1<<" "<<t<<endl;
ans
=0;
int cnt=0;
int p=1;
if(a[1][t]==INF)ans+=a[t][1];
while(1){
cnt
++;
for(int i=0;i<=1;i++){
int j=v[t][i];
if(j!=p){
//cout<<t<<" "<<j<<endl;
if(a[t][j]==INF)ans+=a[j][t];
p
=t;
t
=j;
}
if(t==1)break;
}
if(t==1)break;
if(cnt==n-1)break;

}
return ans;
}
/*int dfs(int t,int p){
int j;
if(t==1){
if(a[t][p]==INF)ans+=a[p][t];
return ans;
}
else for(int i=0;i<1;i++){
int j=v[t][i];
if(j!=p){
if(a[t][j]==INF)ans+=a[j][t];
dfs
(j,t);
}
}
//return ans;
}*/
int main()
{
#ifndef ONLINE_JUDGE
freopen
("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n){
init
();
int x,y,z;
for(int i=1;i<=n;i++){
a
[i][i]=0;
cin
>>x>>y>>z;
a
[x][y]=z;
v
[x].push_back(y);
v
[y].push_back(x);
}
//ans=0;
//cout<<bs(v[1][0]);//<<" "<<bs(v[1][1])<<endl;
//cout<<dfs(v[1][0],1)<<endl;
cout
<<min(bs(v[1][0]),bs(v[1][1]))<<endl;
}
return 0;
}


0 0
原创粉丝点击