HDU1546

题意:给定一些单词,前一个的尾和后一个的头相同时,才表示他们相连

求最短路

View Code
  1 #include<stdio.h>
  2 #include<string>
  3 #include<stdlib.h>
  4 #include<queue>
  5 #include<map>
  6 #include<algorithm>
  7 using namespace std;
  8 const int maxn = 1015;
  9 const int inf = INT_MAX;
 10 //map<string,int>mp;
 11 struct node{
 12     char s[ 505 ];
 13     int val;
 14 }a[ maxn ];
 15 struct Node{
 16     int u,val,next;
 17 }edge[ maxn*maxn ];
 18 int head[ maxn ],cnt;
 19 int dis[ maxn ],vis[ maxn ];
 20 int n;
 21 //int CNT;
 22 void init(){
 23     cnt=0;
 24     //CNT=1;
 25     //mp.clear();
 26     memset( head,-1,sizeof( head ));
 27 }
 28 void addedge( int a,int b,int c ){
 29     edge[ cnt ].u=b;
 30     edge[ cnt ].val=c;
 31     edge[ cnt ].next=head[ a ];
 32     head[ a ]=cnt++;
 33 }
 34 
 35 int bfs(){
 36     queue<int>q;
 37     while( !q.empty() )
 38         q.pop();
 39     for( int i=0;i<maxn;i++ ){
 40         dis[i]=inf;
 41         vis[ i ]=0;
 42     }
 43     int now,next;
 44     now=1;
 45     dis[now]=0;
 46     vis[now]=1;
 47     q.push(now);
 48     while( !q.empty() ){
 49         now=q.front(),q.pop();
 50         vis[now]=0;
 51         for( int i=head[now];i!=-1;i=edge[ i ].next ){
 52             next=edge[ i ].u;
 53             if( dis[next]>dis[now]+edge[i].val ){
 54                 dis[next]=dis[now]+edge[i].val;
 55                 if( vis[next]==0 ){
 56                     vis[next]=1;
 57                     q.push(next);
 58                 }
 59             }
 60         }
 61     }
 62     if( dis[n]==inf )
 63         return -1;
 64     else
 65         return dis[n];
 66     /*
 67     if( dis[get_first_num(a[n-1].s) ]!=inf||dis[get_last_num(a[n-1].s) ]!=inf )
 68         return min( dis[get_first_num(a[n-1].s) ],dis[get_last_num(a[n-1].s) ] );
 69     else 
 70         return -1;
 71     */
 72 }
 73 
 74 int judge( int i,int j ){
 75     int len=strlen(a[i].s);
 76     if( a[i].s[len-4]!=a[j].s[0] )
 77         return -1;
 78     if( a[i].s[len-3]!=a[j].s[1] )
 79         return -1;
 80     if( a[i].s[len-2]!=a[j].s[2] )
 81         return -1;
 82     if( a[i].s[len-1]!=a[j].s[3] )
 83         return -1;
 84     return 1;
 85 }
 86 
 87 int main(){
 88     while( scanf("%d",&n),n ){
 89         init();
 90         //int num1,num2;
 91         for( int i=1;i<=n;i++ ){
 92             scanf("%d%s",&a[i].val,a[i].s);
 93         }
 94         for( int i=1;i<=n;i++ ){
 95             for( int j=1;j<=n;j++ ){
 96                 if( i!=j ){
 97                     /*
 98                     num1=get_last_num(a[i].s);
 99                     num2=get_first_num(a[j].s);
100                     if( num1==num2 ){
101                         addedge( i,j,a[i].val );//mat[num1][num2]=a[i].val;
102                     }
103                     */
104                     if( judge(i,j)==1 )
105                         addedge( i,j,a[i].val );
106                 }
107             }
108         }
109         int flag=bfs();
110         printf("%d\n",flag);
111     }
112     return 0;
113 }

开始想复杂了,以为要把每个单词拆开来。。。

后来看了别人的才发现单词首尾相同即可表示i,j两个相连。。。。

 

转载于:https://www.cnblogs.com/xxx0624/archive/2013/02/21/2920800.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值