zoukankan      html  css  js  c++  java
  • 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
    ");
            else   printf("%d
    ",g[st][ed]);
        }
        return 0;
    }

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

  • 相关阅读:
    帆软学习之开始
    「csp模拟」模拟测试11
    「csp模拟」模拟测试4
    「csp模拟」模拟测试3
    「csp模拟」模拟测试2
    「csp模拟」模拟测试1
    layui表格中日期格式的处理,从数字变日期格式
    Layui下拉框的事件和表格的刷新
    Layui的下拉框
    Layui按钮的禁用和恢复
  • 原文地址:https://www.cnblogs.com/xryz/p/4847988.html
Copyright © 2011-2022 走看看