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]);
        }
    }
  • 相关阅读:
    DOM性能小记
    利用tween.js算法生成缓动效果
    小游戏(锅打灰太狼)
    DOM应用实例(寻找房祖名)
    学习总结——DOM
    图片预加载
    删除src值为空的img标签
    2019-08-17 纪中NOIP模拟B组
    [SCOI2015] 小凸玩矩阵
    [JZOJ4899] 雪之国度
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7095467.html
Copyright © 2011-2022 走看看