lightoj 1049 - One Way Roads(dfs)

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 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 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 (1ai,bin,aibi,1ci100) - 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.

题意:给出n个点, n条边, n条边把n个点组成一个”环”, 但是,有些边方向不对, 导致某些点无法到达其他点,现在告诉你每条边修改方向的代价, 问把n个点组成一个真正的环,使得每个点都可以到达其他任何点的代价是多少。

数据不大,简单的搜索一遍即可,一道简单的dfs

 

#include <iostream>
#include <cstring>
using namespace std;
const int inf = 0X3f3f3f3f;
int map[110][110] , vis[110];
int sum , n;
void dfs(int s , int t , int val , int step) {
    if(s == t && step == n) {
        sum = min(sum , val);
        return ;
    }
    for(int i = 1 ; i <= n ; i++) {
        if(vis[i] != 1) {
            if(map[t][i] == 0 && map[i][t] != 0) {
                vis[i] = 1;
                dfs(s , i , val + map[i][t] , step + 1);
                vis[i] = 0;
            }
            if(map[t][i] != 0) {
                vis[i] = 1;
                dfs(s , i , val , step + 1);
                vis[i] = 0;
            }
        }
    }
}
int main()
{
    int t;
    cin >> t;
    int ans = 0;
    while(t--) {
        ans++;
        cin >> n;
        for(int i = 0 ; i <= n ; i++) {
            for(int j = 0 ; j <= n ; j++) {
                map[i][j] = 0;
            }
        }
        for(int i = 0 ; i < n ; i++) {
            int x , y , z;
            cin >> x >> y >> z;
            map[x][y] = z;
        }
        memset(vis , 0 , sizeof(vis));
        sum = inf;
        dfs(1 , 1 , 0 , 0);
        cout << "Case " << ans << ": " << sum << endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/TnT2333333/p/6071716.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值