zoukankan      html  css  js  c++  java
  • hdu 2112 最短路spfa

    HDU Today

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 11598    Accepted Submission(s): 2705
    http://acm.hdu.edu.cn/showproblem.php?pid=2112

    Problem Description
    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
    这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
    徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
    请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
     
    Input
    输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);
    第二行有徐总的所在地start,他的目的地end;
    接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
    note:一组数据中地名数不会超过150个。
    如果N==-1,表示输入结束。
     
    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
    Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake
    虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <stdlib.h>
    using namespace std;
    #define inf 100000000
    int  p[1005][1005];
    int low[1005];
    char s[200][40];
    int n,vit[1005],maxn;
    void spfa ()
    {
        queue<int>q;
        int i,j,a=0,b=1;
        memset(vit,0,sizeof(vit));
        for(i=0; i<=1000; i++)
            low[i]=inf;
        vit[a]=1;
        q.push(a);
        low[a]=0;
        while(!q.empty())
        {
            int tmp=q.front();
            q.pop();
            for(i=0; i<maxn; i++)
                if(low[i]>low[tmp]+p[tmp][i])
                {
                    low[i]=low[tmp]+p[tmp][i];
                    if(!vit[i])
                    {
                        q.push(i);
                        vit[i]=1;
                    }
                }
            vit[tmp]=0;
        }
        if(low[b]==inf)
            cout<<-1;
        else
            cout<<low[b];
        cout<<endl;
    }
    int main()
    {
        int i,j,len;
        char ll[40],mm[40];
        while(cin>>n)
        {
            if(n==-1)
                break;
            for(i=0; i<1000; i++)
                for(j=0; j<1000; j++)
                    if(i!=j)
                        p[i][j]=inf;
                    else
                        p[i][j]=0;
            cin>>s[0]>>s[1];
            maxn=2;
            for(j=1; j<=n; j++)
            {
                int a,b;
                scanf("%s%s%d",ll,mm,&len);
                int kk=0;
                for(i=0; i<maxn; i++)
                    if(strcmp(s[i],ll)==0)
                    {
                        kk=1;
                        a=i;
                        break;
                    }
                if(!kk)
                {
                    a=maxn;
                    strcpy(s[maxn],ll);
                    maxn++;
                }
                kk=0;
                for(i=0; i<maxn; i++)
                    if(strcmp(s[i],mm)==0)
                    {
                        kk=1;
                        b=i;
                        break;
                    }
                if(!kk)
                {
                    b=maxn;
                    strcpy(s[maxn],mm);
                    maxn++;
                }
                p[a][b]=p[b][a]=len;
            }
            if(strcmp(s[0],s[1])==0)
            {
                cout<<0<<endl;
                continue;
            }
    
    
            spfa();
        }
    }
    View Code
  • 相关阅读:
    ambry集群搭建(无SSL验证的方式)
    接口属性命名不规范的处理方式
    windows下搭建ElasticSearch
    MyBatis-Plus分页插件——PageHelper和IPage原理介绍
    饥荒服务器搭建加mod使用 阿里云服务器 Ubuntu18
    WSL2+Ubuntu20.04桌面功能配置
    设计模式学习总结(Java版)
    Java in 蓝桥杯
    Windows 批处理脚本学习
    Vue风格指南小结
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3432176.html
Copyright © 2011-2022 走看看