• One Way Roads(搜索)


    One Way Roads
    Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

    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). Nextn 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.

    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<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<vector>
    #include<set>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    #define mem(x, y) memset(x, y, sizeof(x))
    typedef long long LL;
    void SI(int &x){scanf("%d", &x);}
    void SI(LL &x){scanf("%lld", &x);}
    int mp[110][110];
    vector<int>vec[110];
    int vis[110];
    int ans;
    int n;
    int dot[110];
    int tp;
    void dfs(int u){
        for(int i = 0; i < vec[u].size(); i++){
            int v = vec[u][i];
            if(vis[v])continue;
            dot[++tp] = v;
            vis[v] = 1;
            dfs(v);
        }
    }
    int main(){
        int T, a, b, c, kase = 0;
        scanf("%d", &T);
        while(T--){
            scanf("%d", &n);
            mem(mp,0);
            mem(vis,0);
            for(int i = 0; i < 110; i++)vec[i].clear();
            for(int i = 0; i < n; i++){
                scanf("%d%d%d", &a, &b, &c);
                mp[a][b] = c;
                vec[a].push_back(b);
                vec[b].push_back(a);
            }
            tp = 0;
            dot[++tp] = 1;
            vis[1] = 1;
            dfs(1);
            int ans1 = 0, ans2 = 0;
            for(int i = 2; i <= tp; i++){
                int u = dot[i - 1], v = dot[i];
                //printf("u = %d v = %d
    ", u, v);
                if(mp[u][v] == 0){
                    ans1 += mp[v][u];
                }
            }
            if(mp[dot[tp]][dot[1]] == 0)
                ans1 += mp[dot[1]][dot[tp]];
                //printf("ans1 = %d
    ", ans1);
            for(int i = tp; i >= 2; i--){
                int u = dot[i], v = dot[i - 1];
            //    printf("u = %d v = %d
    ", u, v);
                if(mp[u][v] == 0){
                    ans2 += mp[v][u];
                }
            }
            if(mp[dot[1]][dot[tp]] == 0)
                ans2 += mp[dot[tp]][dot[1]];
            //printf("ans2 = %d
    ", ans2);
            printf("Case %d: %d
    ", ++kase, min(ans1, ans2));
        }
        return 0;
    }
  • 相关阅读:
    ant的安装和配置
    jmeter3.x的jtx文件解析
    爬虫:网页里元素的xpath结构,scrapy不一定就找的到
    如何实现新浪微博功能:关注某个的发布信息,自动点赞和转发
    性能测试各个指标的含义
    python模块相关
    curl的用法
    第十八章 Python批量管理主机(paramiko、fabric与pexpect)
    死锁产生的原因和解锁的方法
    读写分离死锁解决方案、事务发布死锁解决方案、发布订阅死锁解决方案
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5390715.html
Copyright © 2020-2023  润新知