zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    【嵌入式开发】写入开发板Linux系统-模型S3C6410
    大约cocos2d-X 3.x使用引擎版本自带的物理引擎Physics
    它们的定义PropertyPlaceHolder无法完成更换任务
    [Cocos2d-x]在Cocos2d-x 3.x如何通过版本号WebSocket连接server数据的传输
    Java 内存架构
    类似的微博推断客户关系sql声明
    Kienct与Arduino学习笔记(2) 深度图像与现实世界的深度图的坐标
    etl工具,kettle实现了周期
    Android中自定义checkbox样式
    ndroid网络(4):HttpClient必经之路----使用线程安全的单例模式HttpClient,及HttpClient和Application的融合
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5390715.html
Copyright © 2011-2022 走看看