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

    本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点

    就是个最短路

    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
    
    
    虽然偶尔会迷路,但是因为有了你的帮助
    **和**从此还是过上了幸福的生活。
    
    ――全剧终――
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<map>
     8 const int maxint=9999999;
     9 //printf("----
    ");
    10 using namespace std;
    11 int m,t;
    12 int tot=0;
    13 map<string,int> st;
    14 int c[200][200],dist[200];
    15 void dijkstra(int u,int n)
    16 {
    17     //printf("%d
    ",n);
    18     bool vis[200];
    19     for(int i=1;i<=n;i++)
    20     {
    21         vis[i]=0;
    22         dist[i]=c[u][i];
    23     }
    24     vis[u]=1;
    25     for(int i=2;i<=n;i++)
    26     {
    27         int temp=maxint;
    28         int k=u;
    29         for(int j=1;j<=n;j++)
    30         {
    31             if(!vis[j]&&dist[j]<temp)
    32             {
    33                 temp=dist[j];
    34                 k=j;
    35             }
    36         }
    37         if(temp==maxint)    break;
    38         vis[k]=1;
    39         for(int j=1;j<=n;j++)
    40         {
    41             if(!vis[j]&&c[k][j]<maxint)
    42             {
    43                 if(dist[k]+c[k][j]<dist[j])
    44                     dist[j]=dist[k]+c[k][j];
    45             }
    46         }
    47     }
    48 }
    49 int main()
    50 {
    51     int i,j,k,n;
    52     //freopen("1.in","r",stdin);
    53     while(scanf("%d",&n)!=EOF)
    54     {
    55         bool flag=0;
    56         if(n==-1)   break;
    57         st.clear();
    58         char s[30],e[30];
    59         scanf("%s%s",s,e);
    60         if(strcmp(s,e)==0)
    61         {
    62             flag=1;
    63         }
    64         st[s]=1;
    65         st[e]=2;
    66         tot=3;
    67         for(i=1;i<=150;i++)
    68         {
    69             for(j=1;j<=150;j++)   c[i][j]=c[j][i]=((i==j)?0:maxint);
    70         }
    71         for(i=1;i<=150;i++)
    72         {
    73             dist[i]=maxint;
    74         }
    75         for(i=1;i<=n;i++)
    76         {
    77             int w;
    78             scanf("%s%s%d",s,e,&w);
    79             if(!st[s]) st[s]=tot++;
    80             if(!st[e]) st[e]=tot++;
    81             c[st[s]][st[e]]=c[st[e]][st[s]]=w;
    82         }
    83         dijkstra(1,tot);
    84         if(flag)    printf("0
    ");
    85         else if(dist[2]==maxint) printf("-1
    ");
    86         else
    87             printf("%d
    ",dist[2]);
    88     }
    89     return 0;
    90 }

    2015-04-24:这个是kuangbin模板的,检测了一下,算法里的起点是从0开始的,那个优化代码不知道怎么就wa了,等刷到kuang大神的最短路再看吧

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<map>
     8 using namespace std;
     9 #define MOD 1000000007
    10 const double eps=1e-5;
    11 #define cl(a) memset(a,0,sizeof(a))
    12 #define ts printf("*****
    ");
    13 const int MAXN=1005;
    14 int n,m,tt;
    15 #define typec int
    16 const typec INF=9999999;//防止后面溢出,这个不能太大
    17 int c[MAXN][MAXN],tot=0,dist[200];
    18 bool vis[MAXN];
    19 int pre[MAXN];
    20 void Dijkstra(typec cost[][MAXN],typec lowcost[],int n,int beg)
    21 {
    22     for(int i=0;i<=n;i++)
    23     {
    24         lowcost[i]=INF;vis[i]=false;pre[i]=-1;
    25     }
    26     lowcost[beg]=0;
    27     for(int j=0;j<n;j++)
    28     {
    29         int k=-1;
    30         int Min=INF;
    31         for(int i=1;i<=n;i++)
    32             if(!vis[i]&&lowcost[i]<Min)
    33             {
    34                 Min=lowcost[i];
    35                 k=i;
    36             }
    37         if(k==-1)break;
    38         vis[k]=true;
    39         for(int i=1;i<=n;i++)
    40         if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
    41         {
    42             lowcost[i]=lowcost[k]+cost[k][i];
    43             pre[i]=k;
    44         }
    45     }
    46 }
    47 map<string,int> st;
    48 int main()
    49 {
    50     int i,j,k;
    51     #ifndef ONLINE_JUDGE
    52     freopen("1.in","r",stdin);
    53     #endif
    54     while(scanf("%d",&n)!=EOF)
    55     {
    56         bool flag=0;
    57         if(n==-1)   break;
    58         st.clear();
    59         char s[30],e[30];
    60         scanf("%s%s",s,e);
    61         if(strcmp(s,e)==0)
    62         {
    63             flag=1;
    64         }
    65         st[s]=1;
    66         st[e]=2;
    67         tot=3;
    68         for(i=1;i<=150;i++)
    69         {
    70             for(j=1;j<=150;j++)   c[i][j]=c[j][i]=((i==j)?0:INF);
    71         }
    72         for(i=1;i<=150;i++)
    73         {
    74             dist[i]=INF;
    75         }
    76         for(i=1;i<=n;i++)
    77         {
    78             int w;
    79             scanf("%s%s%d",s,e,&w);
    80             if(!st[s]) st[s]=tot++;
    81             if(!st[e]) st[e]=tot++;
    82             c[st[s]][st[e]]=c[st[e]][st[s]]=w;
    83         }
    84         Dijkstra(c,dist,tot,1);
    85         if(flag)    printf("0
    ");
    86         else if(dist[2]==INF) printf("-1
    ");
    87         else
    88             printf("%d
    ",dist[2]);
    89     }
    90 }
  • 相关阅读:
    octotree神器 For Github and GitLab 火狐插件
    实用篇如何使用github(本地、远程)满足基本需求
    PPA(Personal Package Archives)简介、兴起、使用
    Sourse Insight使用过程中的常使用功能简介
    Sourse Insight使用教程及常见的问题解决办法
    github 遇到Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hosts问题解决
    二叉查找树的C语言实现(一)
    初识内核链表
    container_of 和 offsetof 宏详解
    用双向链表实现一个栈
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4265714.html
Copyright © 2011-2022 走看看