HDU 2112 HDU Today(Floyd)

来源:互联网 发布:淘宝互刷安全么 编辑:程序博客网 时间:2024/06/05 23:51

Description
给出n个公交线路的起点终点名称和用时,问从一个地方到另一个地方所需最短时间
Input
多组用例,每组用例第一行输入一整数n表示公交线路数量,之后输入起点和终点名称,最后n行每行输入一个公交线路的起点和终点以及用时,以n=-1结束输入,每组用例中出现的名称不会超过150个,每个串长不会超过30,n<=10000
Output
输出起点到终点的最短用时,如果起点终点不互通则输出-1
Sample Input
6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1
Sample Output
50
Solution
把字符串映射成编号然后跑一边Floyd求最短路即可
Code

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 222int n,m,a[maxn][maxn];char s[maxn][33],c[maxn];int get(char *c){    for(int i=1;i<=n;i++)        if(strcmp(s[i],c)==0)return i;    strcpy(s[++n],c);    return n;}int main(){    while(~scanf("%d",&m),~m)    {        for(int i=1;i<maxn;i++)            for(int j=1;j<maxn;j++)                a[i][j]=i==j?0:INF;        n=1;        scanf("%s%s",s[1],c);        int u=1,v=get(c);        while(m--)        {            int x,y,temp;            scanf("%s",c);            x=get(c);            scanf("%s",c);            y=get(c);            scanf("%d",&temp);            a[y][x]=a[x][y]=min(a[x][y],temp);        }        for(int k=1;k<=n;k++)            for(int i=1;i<=n;i++)                for(int j=1;j<=n;j++)                    a[i][j]=min(a[i][j],a[i][k]+a[k][j]);        if(a[u][v]==INF)printf("-1\n");        else printf("%d\n",a[u][v]);    }    return 0;}
原创粉丝点击