UVA 1078 dij模板
Posted iEdson
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 1078 dij模板相关的知识,希望对你有一定的参考价值。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <string> #include <queue> #include <vector> #include <algorithm> #include <ctime> using namespace std; //#define EdsonLin #ifdef EdsonLin #define debug(...) fprintf(stderr,__VA_ARGS__) #else #define debug(...) #endif //EdsonLin typedef long long ll; typedef double db; const int inf = 0x3f3f3f; const int MAXN = 110; const int MAXNN = 5e4+10; //const int MAXM = 3e4+100; const int MOD = 1000000007; const db eps = 1e-3; #define PB push_back struct dij{ int n,m; int first[MAXNN]; struct edge{ int st,to,next,dist; }; vector<edge>e; int top; int d[MAXNN]; //s到各节点的距离 int done[MAXNN]; //是否已经被永久标记 int p[MAXNN]; //记录上一条边 struct heapnode{ int st; int dist; bool operator < (const heapnode& rhs) const { return dist>rhs.dist; } }; void init(int n){ this->n = n; memset(first,-1,sizeof(first)); top = 0; e.clear(); } void addedge(int u,int v,int dist){ /*e[top].st = u; e[top].to = v; e[top].dist = dist; e[top].next = first[u]; first[u] = top++;*/ //if(u==0)cout<<dist<<endl; edge a; a.st = u; a.to = v; a.dist = dist; a.next = first[u]; e.PB(a); first[u] = top++; //cout<<first[u]<<endl; // cout<<top<<endl; } void pqdij(int s){ priority_queue<heapnode>Q; heapnode a; for(int i=0;i<n;i++) d[i] = inf; d[s] = 0; memset(done,0,sizeof(done)); a.dist = 0; a.st = s; Q.push(a); while(!Q.empty()){ heapnode x = Q.top(); Q.pop(); int u = x.st; if(done[u])continue; done[u] = 1; //debug("Edson:%d\\n",u); for(int i=first[u];i!=-1;i=e[i].next){ if(d[e[i].to]>d[u]+e[i].dist){ d[e[i].to] = d[u] + e[i].dist; p[e[i].to] = i; a.dist = d[e[i].to]; a.st = e[i].to; Q.push(a); } } } } }solver; const int UP = 0,LEFT = 1,DOWN = 2,RIGHT = 3; const int inv[] = {2,3,0,1}; const int dr[] = {-1,0,1,0}; const int dc[] = {0,-1,0,1}; int grid[MAXN][MAXN][4]; int id[MAXN][MAXN][5]; int idn; int n,m,rs,cs,re,ce; int ID(int r,int c,int dir){ int&x = id[r][c][dir]; if(x==0)x = ++idn; return x; } int readint(){int x;scanf("%d",&x);return x;} bool cango(int r,int c,int dir){ if(r<0||r>=n||c<0||c>=m)return false; return grid[r][c][dir]>0; } int main() { //cout<<inf<<endl; #ifdef EdsonLin //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); int _time_ed = clock(); #endif //EdsonLin int mc = 0; while(cin>>n>>m>>rs>>cs>>re>>ce,n){ rs--;cs--;re--;ce--; for(int i=0;i<n;i++){ for(int j=0;j<m-1;j++){ grid[i][j][RIGHT] = grid[i][j+1][LEFT] = readint(); } if(i!=n-1) for(int j=0;j<m;j++){ grid[i][j][DOWN] = grid[i+1][j][UP] = readint(); } } idn = 0; memset(id,0,sizeof(id)); solver.init(n*m*5+1); /*cout<<grid[0][0][RIGHT]<<endl;*/ for(int dir=0;dir<4;dir++){ if(cango(rs,cs,dir)){ solver.addedge(0,ID(rs+dr[dir],cs+dc[dir],dir),grid[rs][cs][dir]*2); solver.addedge(0,ID(rs+dr[dir],cs+dc[dir],4),grid[rs][cs][dir]*2); } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ for(int dir=0;dir<4;dir++)if(cango(i,j,inv[dir])){ solver.addedge(ID(i,j,dir),ID(i,j,4),grid[i][j][inv[dir]]); if(cango(i,j,dir)) solver.addedge(ID(i,j,dir),ID(i+dr[dir],j+dc[dir],dir),grid[i][j][dir]); } for(int dir=0;dir<4;dir++)if(cango(i,j,dir)){ solver.addedge(ID(i,j,4),ID(i+dr[dir],j+dc[dir],dir),grid[i][j][dir]*2); solver.addedge(ID(i,j,4),ID(i+dr[dir],j+dc[dir],4),grid[i][j][dir]*2); } } } solver.pqdij(0); int ans = solver.d[ID(re,ce,4)]; printf("Case %d: ",++mc); if(ans==inf)printf("Impossible\\n");else printf("%d\\n",ans); } #ifdef EdsonLin debug("time: %d\\n",int(clock()-_time_ed)); #endif //EdsonLin // cout << "Hello world!" << endl; return 0; }
以上是关于UVA 1078 dij模板的主要内容,如果未能解决你的问题,请参考以下文章