lightoj1049 - One Way Roads

1049 - One Way Roads
Time Limit: 0.5 second(s)Memory Limit: 32 MB

Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Dhaka Division decided to keep up with new trends. Formerly alln cities of Dhaka 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 Dhaka 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

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a blank line and an integer n (3 ≤ n ≤ 100) denoting the number of cities (and roads). 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

For each case of input you have to print the case number and 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 Input

Output for Sample Input

4

 

3

1 3 1

1 2 1

3 2 1

 

3

1 3 1

1 2 5

3 2 1

 

6

1 5 4

5 3 8

2 4 15

1 6 16

2 3 23

4 6 42

 

4

1 2 9

2 3 8

3 4 7

4 1 5

Case 1: 1

Case 2: 2

Case 3: 39

Case 4: 0


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int ans;
int map[110][110];
vector<int>visf[110];
vector<int>visl[110];
int main()
{
    int t,i,j,k,n,test=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int ans=0;
        int a,b,c;
        int f1,l1;
        for(i=1;i<=n;++i){
            visf[i].clear();
            visl[i].clear();
        }
        for(i=0;i<n;++i){
            scanf("%d%d%d",&a,&b,&c);
            map[a][b]=c;
            visf[a].push_back(b);
            visl[b].push_back(a);
            f1=a;l1=b;ans+=c;
        }
        int f=f1,r=l1,ans1=0,ans2=0;
        while(r!=f1){
            if(visf[r].empty()){
                for(i=visl[r].size()-1;i>=0;--i){
                    if(visl[r][i]!=f){
                        ans1+=map[visl[r][i]][r];
                        f=r;r=visl[r][i];
                        int temp=-1;
                        for(j=visf[r].size()-1;j>=0;--j){
                            if(visf[r][j]!=f)temp=visf[r][j];
                            visf[r].pop_back();
                        }
                        if(temp!=-1)
                            visf[r].push_back(temp);
                        break;
                    }
                }
            }
            else {
                f=r;r=visf[r][0];
            }
        }
        printf("Case %d: %d\n",test++,min(ans1,ans-ans1));
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值