hdu 2112 HDU Today

输入当中有起点和终点相同的,所以使用map时要注意

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=205;
const int inf=1000;
map<string,int>a;

int main()
{
    int tot,i,j,k,n,ans,t,st,ed,g[N][N];
    char s1[50],s2[50];
    while(~scanf("%d",&n))
    {
        if(n==-1) break;
        for(i=0; i<=200; i++)
            for(j=0; j<=200; j++)
            {
                if(i==j) g[i][j]=0;
                else g[i][j]=inf;
            }
        tot=1;
        a.clear();
        scanf("%s %s ",s1,s2);
        if(a[s1]==0)
        {
            a[s1]=tot;
            tot++;
        }
        st=a[s1];
        if(a[s2]==0)
        {
            a[s2]=tot;
            tot++;
        }
        ed=a[s2];
        for(i=0; i<n; i++)
        {
            scanf("%s %s %d",s1,s2,&t);
            if(a[s1]==0)
            {
                a[s1]=tot;
                tot++;
            }
            if(a[s2]==0)
            {
                a[s2]=tot;
                tot++;
            }
            g[a[s1]][a[s2]]=min(g[a[s1]][a[s2]],t);
            g[a[s2]][a[s1]]=g[a[s1]][a[s2]];
        }

        for(k=1; k<=tot; k++)
            for(i=1; i<=tot; i++)
            {
                for(j=1; j<=tot; j++)
                    if(g[i][j]>g[i][k]+g[k][j])
                        g[i][j]=g[i][k]+g[k][j];
            }

        if(g[st][ed]==inf)
            printf("-1\n");
        else   printf("%d\n",g[st][ed]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-05-13 21:08  xryz  阅读(121)  评论(0编辑  收藏  举报