题目链接:http://poj.org/problem?id=3653
思路:题目意思很简单,就是二维平面上的图,要求起点到终点的最短路。建图略坑,需要坐标映射,化二维为一维。然后就是Dijkstra求最短路了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 using namespace std; 8 #define MAXN 888 9 #define inf 1<<30 10 11 struct Edge{ 12 int v,w; 13 Edge(int vv,int ww):v(vv),w(ww){} 14 }; 15 16 vector<vector<Edge> >G; 17 int n,m; 18 19 int dist[MAXN]; 20 bool mark[MAXN]; 21 22 bool Dijkstra(int vs,int vt) 23 { 24 fill(dist,dist+vt+1,inf); 25 memset(mark,false,sizeof(mark)); 26 dist[vs]=0; 27 priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >que; 28 que.push(make_pair(0,vs)); 29 while(!que.empty()){ 30 pair<int,int>pp=que.top(); 31 que.pop(); 32 int d=pp.first,u=pp.second; 33 if(mark[u])continue; 34 mark[u]=true; 35 for(int i=0;i<G[u].size();i++){ 36 int v=G[u][i].v,w=G[u][i].w; 37 if(mark[v])continue; 38 if(d+w<dist[v]){ 39 dist[v]=w+d; 40 que.push(make_pair(dist[v],v)); 41 } 42 } 43 } 44 return dist[vt]<inf; 45 } 46 47 48 int main() 49 { 50 // freopen("1.txt","r",stdin); 51 int x,y; 52 char ch; 53 while(~scanf("%d%d",&n,&m)){ 54 if(n==0&&m==0)break; 55 G.clear(); 56 G.resize((n+1)*(m+1)+2); 57 for(int i=0;i<n;i++){ 58 for(int j=0;j<m;j++){ 59 scanf("%d %c",&x,&ch); 60 if(x==0)continue; 61 if(ch=='*'){ 62 G[i*(m+1)+j].push_back(Edge(i*(m+1)+j+1,2520/x)); 63 G[i*(m+1)+j+1].push_back(Edge(i*(m+1)+j,2520/x)); 64 }else if(ch=='>'){ 65 G[i*(m+1)+j].push_back(Edge(i*(m+1)+j+1,2520/x)); 66 }else if(ch=='<') 67 G[i*(m+1)+j+1].push_back(Edge(i*(m+1)+j,2520/x)); 68 } 69 for(int j=0;j<=m;j++){ 70 scanf("%d %c",&x,&ch); 71 if(x==0)continue; 72 if(ch=='*'){ 73 G[i*(m+1)+j].push_back(Edge(i*(m+1)+j+m+1,2520/x)); 74 G[i*(m+1)+j+m+1].push_back(Edge(i*(m+1)+j,2520/x)); 75 }else if(ch=='^'){ 76 G[i*(m+1)+j+m+1].push_back(Edge(i*(m+1)+j,2520/x)); 77 }else if(ch=='v') 78 G[i*(m+1)+j].push_back(Edge(i*(m+1)+j+m+1,2520/x)); 79 } 80 } 81 for(int j=0;j<m;j++){ 82 scanf("%d %c",&x,&ch); 83 if(x==0)continue; 84 if(ch=='*'){ 85 G[n*(m+1)+j].push_back(Edge(n*(m+1)+j+1,2520/x)); 86 G[n*(m+1)+j+1].push_back(Edge(n*(m+1)+j,2520/x)); 87 }else if(ch=='>'){ 88 G[n*(m+1)+j].push_back(Edge(n*(m+1)+j+1,2520/x)); 89 }else if(ch=='<'){ 90 G[n*(m+1)+j+1].push_back(Edge(n*(m+1)+j,2520/x)); 91 } 92 } 93 if(Dijkstra(0,(n+1)*(m+1)-1)){ 94 printf("%d blips ",dist[(n+1)*(m+1)-1]); 95 }else 96 puts("Holiday"); 97 } 98 return 0; 99 }