LightOJ - 1049 One Way Roads

本文探讨了如何通过单向交通调整最小化成本,确保所有城市间可达性,重点介绍了输入参数、算法流程及示例解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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 all n 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 nroads, 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 costsci.

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

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

Sample Output

Case 1: 1

Case 2: 2

Case 3: 39

Case 4: 0


题解:就是形成一个环,把它想成一个无向环,后找一个方向一直搜索,找相反的权值和,与总权值相减比较。


#include <string.h>
#include <stdio.h>

int pic[101][101];
int mark[101];
int n;int flag;

int dfs(int pos,int pre){
    mark[pos] = 1;
    if(flag==1 && pos==1)return 0;
    for(int i = 1; i <= n; i++)
        if( (pos!=1&&pre!=i&&pic[pos][i]!=0&&i==1)|| (!mark[i]&&pic[pos][i]!=0&&pre!=i)){
            flag = 1;
            return dfs(i,pos);
        }
    for(int i = 1; i <= n; i++)
        if( (pos!=1&&pre!=i&&pic[i][pos]!=0&&i==1)|| (!mark[i]&&pic[i][pos]!=0&&pre!=i)){
            flag = 1;
            return pic[i][pos]+dfs(i,pos);
        }
}

int main(){
    int T;
    int sum;
    scanf("%d",&T);
    for(int i = 1;i <=T;i++){
        scanf("%d",&n);
        sum = 0;
        memset(pic,0,sizeof(pic));
        memset(mark,0,sizeof(mark));
        for(int j = 0 ; j < n; j++){
            int t1,t2,t3;
            scanf("%d%d%d",&t1,&t2,&t3);
            pic[t1][t2] = t3;
            sum += t3;
        }
        flag = 0;
        int ans = dfs(1,0);
        if(ans>sum-ans)ans = sum-ans;
        printf("Case %d: %d\n",i,ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值