其他分享
首页 > 其他分享> > POJ - 1847 Tram

POJ - 1847 Tram

作者:互联网

POJ - 1847 题目链接

思路

题意比较简单,关键在于如何转化为图的过程。

代码

//Powered by CK 2020/04/03
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 110;
const int INF = 0x3f3f33f;
int ans[N][N], rt, st, n;
void init() {
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(i == j)  ans[i][j] = 0;
            else    ans[i][j] = INF;
}
void Floyd() {
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);
    if(ans[rt][st] == INF)  puts("-1");
    else    printf("%d\n", ans[rt][st]);
}
int main() {
    // freopen("in.txt", "r", stdin);
    int x, y, w;
    while(scanf("%d %d %d", &n, &rt, &st) != EOF) {
        init();
        for(int i = 1; i <= n; i++) {
            int sum;
            scanf("%d", &sum);
            for(int j = 1; j <= sum; j++) {
                scanf("%d", &y);
                if(j == 1)  ans[i][y] = 0;
                else    ans[i][y] = 1;
         }
        }
        Floyd();
    }
    return 0;
}

标签:const,边权值,Tram,1847,int,POJ,开关,include,链接
来源: https://www.cnblogs.com/lifehappy/p/12626932.html