zoukankan      html  css  js  c++  java
  • HDU 2122 HDU Today【Floyd】

    题意:给出n条路,起点和终点,问最短距离

    用map处理一下地名,再用floyd

    可是不懂的是:为什么INF定义成0x7fffffff就输出一堆奇怪的东西,改成100000000就可以了

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 #define mod=1e9+7;
    12 using namespace std;
    13 
    14 typedef long long LL;
    15 const int INF = 10000000;
    16 const int maxn=1005;
    17 map<string,int> mp;
    18 int d[maxn][maxn];
    19 
    
    20 int main(){
    21     int n,i,j,k,w;
    22     string st,en,s1,s2;
    23     while(scanf("%d",&n)!=EOF&&(n!=-1)){
    24         mp.clear();
    25         int cnt=0;
    26         
    27         for(i=0;i<maxn;i++){
    28             for(j=0;j<maxn;j++){
    29                 if(i==j) d[i][j]=0;
    30                 else d[i][j]=INF;
    31             }
    32         }
    33         
    34         cin>>st>>en;
    35         if(!mp[st]) mp[st]=cnt++;
    36         if(!mp[en]) mp[en]=cnt++;
    37         
    38         for(i=0;i<n;i++){
    39             cin>>s1>>s2>>w;
    40             if(!mp[s1]) mp[s1]=cnt++;
    41             if(!mp[s2]) mp[s2]=cnt++;
    42             
    43             if(d[mp[s1]][mp[s2]]>w) d[mp[s1]][mp[s2]]=d[mp[s2]][mp[s1]]=w;            
    44         }
    45         
    46         for(k=0;k<cnt;k++)
    47          for(i=0;i<cnt;i++)
    48           for(j=0;j<cnt;j++)
    49            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    50            
    51            if(d[mp[st]][mp[en]]==INF) printf("-1
    ");
    52            else printf("%d
    ",d[mp[st]][mp[en]]);                
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    P3916 图的遍历
    P1656 炸铁路
    P6722 「MCOI-01」Village 村庄
    P1341 无序字母对
    P1072 [NOIP2009 提高组] Hankson 的趣味题
    10大主流自动化测试工具介绍
    Altium Designer中off grid pin问题的解决方法
    Easylogging++的使用及扩展
    博客园粒子特效稳定版
    C#中使用jieba.NET、WordCloudSharp制作词云图
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4401048.html
Copyright © 2011-2022 走看看