原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2722
分析:简单最短路,读入数据烦。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<vector> 8 #define maxn 450 9 #define inf 0xfffffff 10 using namespace std; 11 int blip[maxn]; 12 int n,m; 13 bool vis[maxn]; 14 struct edge 15 { 16 int to,w; 17 edge(int x,int y) 18 { 19 to=x;w=y; 20 } 21 }; 22 vector<edge>e[maxn]; 23 queue<int>q; 24 void spfa(const int s) 25 { 26 while(!q.empty())q.pop(); 27 for(int i=0;i<(n+1)*(m+1);i++) 28 { 29 vis[i]=false; 30 blip[i]=inf; 31 } 32 vis[s]=true;blip[s]=0; 33 q.push(s); 34 while(!q.empty()) 35 { 36 int u=q.front();q.pop(); 37 for(int i=0;i<e[u].size();i++) 38 { 39 if(blip[e[u][i].to]>blip[u]+e[u][i].w) 40 { 41 blip[e[u][i].to]=blip[u]+e[u][i].w; 42 if(!vis[e[u][i].to]) 43 { 44 vis[e[u][i].to]=true; 45 q.push(e[u][i].to); 46 } 47 } 48 } 49 vis[u]=false; 50 } 51 } 52 int main() 53 { 54 while(~scanf("%d%d",&n,&m)) 55 { 56 if(n==0||m==0)break; 57 for(int i=0;i<maxn;i++)e[i].clear(); 58 for(int i=0;i<=2*n;i++) 59 { 60 int pp=m;if(i&1)pp++; 61 for(int j=0;j<pp;j++) 62 { 63 int v;char c;int tmp; 64 scanf("%d %c",&v,&c); 65 if(v!=0) 66 { 67 if(c=='*'&&i%2==0) 68 { 69 tmp=i/2*(m+1)+j; 70 e[tmp].push_back(edge(tmp+1,2520/v)); 71 e[tmp+1].push_back(edge(tmp,2520/v)); 72 } 73 else if(c=='*'&&(i&1)) 74 { 75 tmp=i/2*(m+1)+j; 76 e[tmp].push_back(edge(tmp+m+1,2520/v)); 77 e[tmp+m+1].push_back(edge(tmp,2520/v)); 78 } 79 else if(c=='>')e[i/2*(m+1)+j].push_back(edge(i/2*(m+1)+j+1,2520/v)); 80 else if(c=='<')e[i/2*(m+1)+j+1].push_back(edge(i/2*(m+1)+j,2520/v)); 81 else if(c=='v')e[i/2*(m+1)+j].push_back(edge(i/2*(m+1)+j+m+1,2520/v)); 82 else if(c=='^')e[i/2*(m+1)+j+m+1].push_back(edge(i/2*(m+1)+j,2520/v)); 83 } 84 } 85 } 86 spfa(0); 87 if(blip[(n+1)*(m+1)-1]==inf)cout<<"Holiday "; 88 else printf("%d blips ",blip[(n+1)*(m+1)-1]); 89 } 90 return 0; 91 }