• 【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)


    http://acm.hdu.edu.cn/showproblem.php?pid=2112

    这道题给了一个将字符串与int对应的思路,就是使用map

    这道题答案对了,但是没有AC,我也不知道为什么。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <queue>
     5 #include <vector>
     6 #include <map>
     7 using namespace std;
     8 const int Ni = 10000;
     9 const int INF = 1<<27;
    10 
    11 typedef pair<int,int> pa;
    12 
    13 int dis[Ni],n;//dis使用1~n的部分
    14 
    15 vector<pair<int,int> > eg[Ni];
    16 
    17 void Dijkstra(int s)
    18 {
    19           int i,j;
    20           cout<<n<<endl;
    21           for(i=0;i<=n;i++)//要到n
    22                     dis[i] = INF;
    23 
    24           priority_queue<pa>q;  //优先级队列:小顶堆
    25           dis[s] = 0;
    26           q.push(make_pair(s,dis[s]));
    27           while(!q.empty())
    28           {
    29                     pa x = q.top();
    30                     q.pop();
    31                     int w = x.first;
    32                     for(j = 0;j<eg[w].size();j++)//遍历x的所有邻接点
    33                     {
    34                               pa y = eg[w][j];//y是x的邻接点
    35                               int u = y.first;
    36                               if(dis[u]>x.second+y.second)
    37                               {
    38                                         dis[u] = x.second+y.second;
    39                                         q.push(make_pair(u,dis[u]));
    40                               }
    41                     }
    42           }
    43 
    44 }
    45 
    46 
    47 int main()
    48 {
    49           int m,d;//关系个数
    50           string a,b;
    51           map<string,int> mp;
    52           while(cin>>n && n!=-1)
    53           {
    54                     mp.clear();
    55                     for(int i = 0;i<=n;i++)
    56                               eg[i].clear();//初始化
    57                     cin>>a>>b;
    58                     bool flag = false;
    59                     if(a==b)
    60                               flag = true;
    61                     mp[a] = 1;
    62                     mp[b] = 2;
    63                     int k = 3;
    64                     int w,u;
    65                     int t = n;
    66                     while(t--)
    67                     {
    68                               cin>>a>>b>>d;
    69                               if(!mp[a])
    70                               {
    71                                         w = k;
    72                                         mp[a] = k++;
    73                               }
    74                               if(!mp[b])
    75                               {
    76                                         u = k;
    77                                         mp[b] = k++;
    78                               }
    79                               eg[mp[a]].push_back(make_pair(mp[b],d));
    80                               eg[mp[b]].push_back(make_pair(mp[a],d));
    81                     }
    82                     if(flag)
    83                     {
    84                               cout<<"0
    ";
    85                               continue;
    86                     }
    87                     Dijkstra(1);
    88                     if(dis[2]!=INF)
    89                               cout<<dis[2]<<endl;
    90                     else
    91                               cout<<"-1
    ";
    92           }
    93 
    94           return 0;
    95 }

    第二次写,依旧WA

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <string>
      4 #include <queue>
      5 #include <vector>
      6 #include <map>
      7 using namespace std;
      8 
      9 #define MEM(a,v) memset (a,v,sizeof(a))
     10 // a for address, v for value
     11 
     12 #define max(x,y) ((x)>(y)?(x):(y))
     13 #define max(x,y) ((x)>(y)?(x):(y))
     14 
     15 #define debug printf("!
    ")
     16 
     17 const int L = 10010;
     18 const int INF = 1<<27;
     19 
     20 int n;
     21 
     22 struct node
     23 {
     24           int x,w;
     25 
     26           node(){}
     27           node(int a,int b)
     28           { x= a;w =b;}
     29 
     30           bool operator <(const node& a) const
     31           {
     32                     return w<a.w;
     33           }
     34 };
     35 
     36 vector<node> va[L];
     37 int dis[L];
     38 
     39 
     40 void Dijkstra(int s)
     41 {
     42           int i;
     43           for(i=0;i<=n;i++)
     44                     dis[i] = INF;
     45 
     46           dis[s] = 0;
     47 
     48           priority_queue<node> q;
     49           q.push(node(s,dis[s]));
     50 
     51           while(!q.empty())
     52           {
     53                     node t = q.top();
     54                     q.pop();
     55 
     56                     int x = t.x;
     57 
     58                     for(i = 0;i<va[x].size();i++)
     59                     {
     60                               node y = va[x][i];
     61                               if(dis[y.x]>y.w+t.w)
     62                               {
     63                                         dis[y.x] = y.w+t.w;
     64                                         q.push(node(y.x,dis[y.x]));
     65                               }
     66                     }
     67           }
     68 }
     69 
     70 
     71 
     72 int main()
     73 {
     74           int i,j;
     75 
     76           bool flag;
     77 
     78           char begin[32],end[32];
     79           char c[32],d[32];
     80 
     81           map<string,int> mp;
     82 
     83           int w,k;
     84 
     85           while(scanf("%d",&n)!=EOF && n!=-1)
     86           {
     87 
     88                     mp.clear();
     89 
     90                     for(i = 0;i<=n;i++)
     91                               va[i].clear();
     92 
     93                     flag = false;
     94 
     95                     scanf("%s%s",begin,end);
     96 
     97                     if(strcmp(begin,end) == 0)
     98                               flag = true;
     99 
    100                     mp[begin] = 1;
    101                     mp[end] = 2;
    102 
    103                     k = 3;
    104 
    105                     for(i = 0;i<n;i++)
    106                     {
    107                               scanf("%s%s%d",c,d,&w);
    108 
    109                               if(!mp[c])
    110                                         mp[c] = k++;
    111                               if(!mp[d])
    112                                         mp[d] = k++;
    113 
    114                               va[mp[c]].push_back(node(mp[d],w));
    115                     }
    116 
    117 
    118                     if(flag)
    119                     {
    120                               printf("0
    ");
    121                               continue;
    122                     }
    123                     Dijkstra(1);
    124                     if(dis[2]!=INF)
    125                               printf("%d
    ",dis[2]);
    126                     else
    127                               printf("-1
    ");
    128           }
    129 
    130           return 0;
    131 }

    UPDATE:

    AC,780MS

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <cctype>
    #include <vector>
    #include <iterator>
    #include <set>
    #include <map>
    #include <sstream>
    using namespace std;
    
    #define mem(a,b) memset(a,b,sizeof(a))
    #define pf printf
    #define sf scanf
    #define spf sprintf
    #define pb push_back
    #define debug printf("!
    ")
    #define INF 10000
    #define MAXN 5010
    #define MAX(a,b) a>b?a:b
    #define blank pf("
    ")
    #define LL long long
    #define ALL(x) x.begin(),x.end()
    #define INS(x) inserter(x,x.begin())
    #define pqueue priority_queue
    
    #define INF 0x3f3f3f3f
    
    int n,k;
    int g[155][155];//map
    int vis[155],d[155];
    char name[155][35],str1[35],str2[35];
    
    int dijkstra(int v)
    {
        int i,j,min,dir;
        for(i=0;i<k;i++)
        {
            vis[i]=0;
            d[i]=g[v][i];
        }
    
        vis[v] = 1;
        d[v]=0;
    
        for(i = 0;i<k;i++)
        {
            min = INF;
            for(j=0;j<k;j++)
            {
                if(!vis[j] && min>d[j])
                {
                    min = d[j];
                    dir = j;
                }
            }
            if(min==INF) break;
            vis[dir]=1;
            for(j=0;j<k;j++)
            {
                if(!vis[j] && d[j] > d[dir]+g[dir][j])
                    d[j] = d[dir]+g[dir][j];
            }
        }
        return d[1];
    }
    
    
    int main()
    {
        int a,num1,num2,i,j;
        while(sf("%d",&n)==1 && n!=-1)
        {
            mem(g,63);
            sf("%s %s",name[0],name[1]);
            k=2;
            for(i = 0;i<n;i++)
            {
                sf("%s %s %d",str1,str2,&a);
                for(j = 0;j<k;j++)
                {
                    if(strcmp(str1,name[j])==0)
                    {
                        num1 = j;
                        break;
                    }
                }
                if(j==k)
                {
                    num1 = k;
                    strcpy(name[k++],str1);
                }
                for(j = 0;j<k;j++)
                {
                    if(strcmp(str2,name[j])==0)
                    {
                        num2 = j;
                        break;
                    }
                }
                if(j==k)
                {
                    num2 = k;
                    strcpy(name[k++],str2);
                }
                if(g[num1][num2]>a)
                    g[num1][num2]=a;
                if(g[num2][num1]>a)
                    g[num2][num1]=a;
    
            }
            if(strcmp(name[0],name[1])==0)
                pf("0
    ");
            else
            {
                i = dijkstra(0);
                if(i>=INF)
                    pf("-1
    ");
                else
                    pf("%d
    ",i);
            }
        }
    }
  • 相关阅读:
    asp.net中合并DataGrid行
    将Asp.Net页面输出到EXCEL里去····
    清空Sql数据库日志等操作
    opengl 教程(14) 摄像机控制(1)
    awk使用技巧
    opengl 教程(10) index draw
    opengl 教程(12) 投影矩阵
    opengl 教程(9) 顶点属性插值
    opengl 教程(15) 摄像机控制(2)
    opengl 教程(11) 平移/旋转/缩放
  • 原文地址:https://www.cnblogs.com/qlky/p/4971840.html
Copyright © 2020-2023  润新知