HDU 2112 HDU Today(Floyd)

61 篇文章 0 订阅

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 222
int 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值