zoukankan      html  css  js  c++  java
  • 九度OJ 1162:I Wanna Go Home

    题目描述:

        The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible. 
        "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
        Would you please tell Mr. M at least how long will it take to reach his sweet home?

    输入:

        The input contains multiple test cases.
        The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
        The second line contains one integer M (0<=M<=10000), which is the number of roads.
        The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
        Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i. 
        To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2. 
        Note that all roads are bidirectional and there is at most 1 road between two cities.
    Input is ended with a case of N=0.

    输出:

        For each test case, output one integer representing the minimum time to reach home.
        If it is impossible to reach home according to Mr. M's demands, output -1 instead.

    样例输入:
    2
    1
    1 2 100
    1 2
    3
    3
    1 2 100
    1 3 40
    2 3 50
    1 2 1
    5
    5
    3 1 200
    5 3 150
    2 5 160
    4 3 170
    4 2 170
    1 2 2 2 1
    0
    样例输出:
    100
    90
    540
    来源:
    2011年北京大学计算机研究生机试真题
    #include <cstdio>
    #include <vector>
    #define N 601
    #define M 10001
    using namespace std;
    struct Edge{
        int to;
        int cost;
    };
    int main(){
        int n;// city number
        int m;//road number
        int flag[N];
        int dist[N];
        bool mark[N];
        vector<Edge> graph[N];
        while(scanf("%d",&n) != EOF){
            if(n == 0)break;
            for(int i = 1;i <= n;i++)graph[i].clear();
            scanf("%d",&m);
            for(int i = 0;i < m;i++){
                int a,b,q;
                scanf("%d%d%d",&a,&b,&q);
                Edge tmp;tmp.to = b;tmp.cost = q;
                graph[a].push_back(tmp);
                tmp.to = a;
                graph[b].push_back(tmp);
            }
            for(int i = 1;i <= n;i++){
                scanf("%d",&flag[i]);
                dist[i] = -1;
                mark[i] = false;
            }
            int s = 1;
            //Dijstra
            dist[s] = 0;
            mark[s] = true;
            int newP = s;
    
            for(int i = 1;i < n;i++){
                for(int j = 0;j < graph[newP].size();j++){
                    int t = graph[newP][j].to;
                    int d = graph[newP][j].cost;
                    if(flag[newP] == 2 && flag[t] == 1)continue;
                    if(mark[t] == true)continue;
                    if(dist[t] == -1 || dist[t] > dist[newP] + d){
                        dist[t] = dist[newP] + d;
                    }
                }
                int min = 0x3f3f3f;
                for(int j = 1;j <= n;j++){
                    if(mark[j]== true || dist[j] == -1)continue;
                    if(dist[j] < min){
                        min = dist[j];
                        newP = j;
                    }
                }
                mark[newP] = true;
            }
            printf("%d
    ",dist[2]);
        }
    }
  • 相关阅读:
    如何学习WindDbg
    如何在程序中嵌入google的V8 Javascript引擎
    理解程序内存
    如何学习Windows编程
    如何让窗口控件半透明
    Sessions, Window Stations and Desktops
    QQ截图时窗口自动识别的原理
    为什么设计模式在C++社区没有Java社区流行?
    当年写的俄罗斯方块
    如何判断一个C++对象是否在堆上
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7095467.html
Copyright © 2011-2022 走看看