zoukankan      html  css  js  c++  java
  • hdu1546+spfa

    没啥好讲的,直接构图就是。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 #include<queue>
     5 using namespace std;
     6 char str[1100][110];
     7 int sw[1100],n;
     8 const int inf=1<<30;
     9 struct node
    10 {
    11     int v,w;
    12     node(int vv,int ww):v(vv),w(ww){}
    13     node(){}
    14 };
    15 vector<vector<node> > g;
    16 int dist[1100],vis[1100];
    17 void spfa()
    18 {
    19     int i;
    20     for(i=0;i<n;i++)
    21     {
    22         dist[i]=inf;
    23         vis[i]=0;
    24     }
    25     dist[0]=0;
    26     queue<int> q;
    27     q.push(0);
    28     vis[0]=1;
    29     while(!q.empty())
    30     {
    31         int z=q.front();
    32         for(i=0;i<g[z].size();i++)
    33         {
    34             int v2=g[z][i].v;
    35             if(dist[z]+g[z][i].w<dist[v2])
    36             {
    37                 dist[v2]=dist[z]+g[z][i].w;
    38                 if(!vis[v2])
    39                 {
    40                     q.push(v2);
    41                     vis[v2]=1;
    42                 }
    43             }
    44         }
    45         q.pop();
    46         vis[z]=0;
    47     }
    48 }
    
    49 int main()
    50 {
    51     int i,j,k1,k2;
    52     while(scanf("%d",&n)!=EOF&&n!=0)
    53     {
    54         for(i=0;i<n;i++)
    55             scanf("%d %s",&sw[i],str[i]);
    56         g.clear();
    57         g.resize(1100);
    58         for(i=0;i<n;i++)
    59         {
    60             for(j=0;j<n;j++)
    61             {
    62                 if(i!=j)
    63                 {
    64                     int len=strlen(str[i]);
    65                     for(k1=0,k2=len-4;k1<4&&k2<len;k1++,k2++)
    66                     {
    67                         if(str[i][k2]!=str[j][k1])
    68                             break;
    69                     }
    70                     if(k1>=4)
    71                         g[i].push_back(node(j,sw[i]));
    72                 }
    73             }
    74         }
    75         //以上均为构图
    76         spfa();
    77         if(dist[n-1]>=inf) printf("-1
    ");
    78         else printf("%d
    ",dist[n-1]);
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    .net Application的目录
    (转载).NET中RabbitMQ的使用
    (转载)RabbitMQ消息队列应用
    说说JSON和JSONP
    SQL Server中的事务与锁
    StackExchange.Redis Client(转载)
    正则语法的查询,这是纯转载的,为了方便查询
    Regex的性能问题
    解决json日期格式问题的办法
    BenchmarkDotNet(性能测试)
  • 原文地址:https://www.cnblogs.com/mt522/p/5383747.html
Copyright © 2011-2022 走看看