CodeForces 24A-Ring road

来源:互联网 发布:刚开淘宝店卖什么好 编辑:程序博客网 时间:2024/06/01 16:41
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.

Examples
input
31 3 11 2 13 2 1
output
1
input
31 3 11 2 53 2 1
output
2
input
61 5 45 3 82 4 151 6 162 3 234 6 42
output
39
input
41 2 92 3 83 4 74 1 5
output
0

#include <iostream>#include <string.h>#include <stdio.h>using namespace std;int map[101][101];int mark[101][101];int visit[101];int n,m=0,flag=0,k=0;void dfs(int pos){    k++;    visit[pos]=1;    if(k==n)    {        m+=map[pos][1];    }    for(int i = 1; i <= n; i++)    {        if(mark[pos][i]==1&&!visit[i])        {            m+=map[pos][i];            if(k==n) flag=1;            if(flag==1) return ;            dfs(i);        }    }}int main(){    int sum;    while (~scanf("%d",&n))    {        sum = 0;        memset(map,0,sizeof(map));        memset(mark,0,sizeof mark);        memset(visit,0,sizeof visit);        for(int j = 0 ; j < n; j++)        {            int t1,t2,t3;            scanf("%d%d%d",&t1,&t2,&t3);            map[t2][t1] = t3;            mark[t1][t2]=1;            mark[t2][t1]=1;            sum += t3;        }        dfs(1);        if(m>sum-m) m = sum-m;        printf("%d\n",m);    }}

0 0
原创粉丝点击