http://acm.hdu.edu.cn/showproblem.php?pid=4360
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <cstdio> 5 using namespace std; 6 const long long inf = 1ll<<57; 7 const int Ni = 1320*2; 8 struct node{ 9 int v,w,e; 10 node(){} 11 node(int a,int b,int c) {v=a;w=b;e=c;} 12 }; 13 struct qnode{ 14 int v,e; 15 qnode(){} 16 qnode(int a,int b) {v=a;e=b;} 17 }; 18 vector<node> eg[Ni]; 19 long long d[Ni][4]; 20 int num[Ni][4]; 21 bool vis[Ni][4]; 22 int n,m; 23 void spfa(int s) 24 { 25 int i,j; 26 for(i=1;i<=n;i++) 27 for(j=0;j<=3;j++) 28 d[i][j]=inf,num[i][j]=0,vis[i][j]=0; 29 queue<qnode> q; 30 int el=3,er; 31 d[s][el]=0; 32 vis[s][el]=1; 33 q.push(qnode(s,el)); 34 while(!q.empty()) 35 { 36 qnode qn=q.front(); q.pop(); 37 int u=qn.v; 38 el=qn.e;vis[u][el]=0; 39 for(i=0;i<eg[u].size();i++) 40 { 41 node nd=eg[u][i]; 42 int v=nd.v,w=nd.w; 43 er=nd.e; 44 if((el+1)%4==er&&(d[u][el]+w<d[v][er]||d[v][er]==0)) 45 { 46 d[v][er]=d[u][el]+w; 47 num[v][er]=num[u][el]; 48 if(er==3) num[v][er]++; 49 if(!vis[v][er]) 50 { 51 q.push(qnode(v,er)); 52 vis[v][er]=1; 53 } 54 } 55 else if((el+1)%4==er&&(d[u][el]+w==d[v][er]||d[v][er]==0)&&num[u][el]>=num[v][er]) 56 { 57 num[v][er]=num[u][el]; 58 if(er==3) num[v][er]++; 59 60 if(!vis[v][er]) 61 { 62 q.push(qnode(v,er)); 63 vis[v][er]=1; 64 } 65 } 66 } 67 } 68 } 69 int main() 70 { 71 int t,cs=1; 72 char str[5]; 73 int u,v,w; 74 node nd; 75 cin>>t; 76 while(t--) 77 { 78 scanf("%d%d",&n,&m); 79 for(int i=1;i<=n;i++) eg[i].clear(); 80 while(m--) 81 { 82 scanf("%d%d%d%s",&u,&v,&w,str); 83 int e;if(str[0]=='L') e=0; 84 else if(str[0]=='O') e=1; 85 else if(str[0]=='V') e=2; 86 else e=3; 87 eg[u].push_back(node(v,w,e)); 88 eg[v].push_back(node(u,w,e)); 89 } 90 spfa(1); 91 if(d[n][3]!=inf&&d[n][3]!=0) 92 printf("Case %d: Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",cs++,d[n][3],num[n][3]); 93 else 94 printf("Case %d: Binbin you disappoint Sangsang again, damn it!\n",cs++); 95 } 96 return 0; 97 }
测试数据:
3
5 5
1 2 1 L
2 3 1 O
3 4 1 V
4 5 2 E
4 5 1 E
1 4
1 1 1 L
1 1 1 O
1 1 1 V
1 1 1 E
9 12
1 3 2 L
3 5 2 O
5 7 2 V
7 9 2 E
1 2 1 L
2 3 1 O
3 4 1 V
4 5 1 E
5 6 1 L
6 7 1 O
7 8 1 V
8 9 1 E
ans:
4 1
4 1
8 2