27. I Wanna Go Home

作者: IceFrozen | 来源:发表于2019-01-17 14:15 被阅读0次
    题目描述

    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.

    示例1

    输入

    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
    
    思路

    到了北大机试部分,陆续出现英文题干的题目了,读起来有些耗时,这道题涉及图的算法,根据输入的城市和路径建立邻接矩阵表示的图,然后用求多源最短路径的算法求出最短路线。

    解法
    #include <stdio.h>
    #include <malloc.h>
    #include <limits.h>
    
    #define INF INT_MAX / 2    //设置一个无穷大数,另其为 int 最大值的一半,之所以用一半是因为 floyd 算法中有相加判断的过程,防止加法溢出导致判断失效
    
    void floyd(int **G, int n) {    //求多源最短路径的 floyd 算法的经典写法
        for (int k = 1; k <= n; k++)
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    if (G[i][j] > G[i][k] + G[k][j])
                        G[i][j] = G[i][k] + G[k][j];
    }
    
    int main() {
        int N;    //城市个数
        int M;    //路的个数
        while(~scanf("%d %d", &N, &M)) {
            int *leader = (int *) malloc (sizeof(int) * (N + 1));    //记录每个城市的领导者,为了直观不用第 0 个元素,所以建立 N + 1 维
            int **G = (int **) malloc (sizeof(int *) * (N + 1));    //以下三行动态分配了一个长宽都为 N + 1 的二维数组,用来存 N * N 的图 G
            for (int i = 0; i < N + 1; i++)
                G[i] = (int *) malloc (sizeof(int) * (N + 1));
            for (int i = 1; i <= N; i++)    //初始化图
                for (int j = 1; j <=N; j++) {
                    if (i == j)
                        G[i][j] = 0;
                    else 
                        G[i][j] = INF;
                }
            for (int i = 0, x, y, k; i < M; i++) {
                scanf("%d %d %d", &x, &y, &k);
                if(G[x][y] > k)
                    G[x][y] = G[y][x] = k;
            }
            for (int i = 1; i <= N; i++)
                scanf("%d", &leader[i]);
            for (int i = 1; i <= N; i++)
                for (int j = 1; j<= N; j++) {
                    if (leader[i] == 2 && leader[j] == 1)    //题目规定只能有一条跨越不同城市的路径,也就是说只能有一次 1 -> 2,所以把 2 -> 1 的路径全部封死
                        G[i][j] = INF;
                }
            floyd(G, N);
            if (G[1][2] == INF)
                printf("-1\n");
            else
                printf("%d\n", G[1][2]);
            free(leader);
            free(G);
        }
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:27. I Wanna Go Home

        本文链接:https://www.haomeiwen.com/subject/mzlidqtx.html