(i,j)表示当前在结点j,还剩下i次使用魔法的机会。
以(i,j)为结点建图,求最短路。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); //freopen("d:\\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=‘ ‘&&ch!=‘\n‘)return ch; } return EOF; } const int maxn=500005; int n,k,A,B; struct HeapNode { int d,u; bool operator < (const HeapNode &ant) const { return d>ant.d; } }; struct Edge { int from,to; int dist; }; struct Dijksta { int n; set<int> g[maxn]; int done[maxn]; int d[maxn]; void init(int n) { this->n=n; for(int i=0;i<=n;i++) g[i].clear(); } void add(int u,int v,int w) { g[u].insert(v); } void solve(int s) { for(int i=0;i<=n;i++) d[i]=max(A,B); memset(done,0,sizeof(done)); d[s]=0; priority_queue<HeapNode> q; q.push((HeapNode){0,s}); while(!q.empty()) { HeapNode x=q.top();q.pop(); if(done[x.u])continue; int u=x.u; done[u]=1; for(int v=1;v<=n;v++)if(v!=u) { int w; if(g[u].find(v)!=g[u].end()) { w=A; }else w=B; if(w+d[u]<d[v]) { d[v]=w+d[u]; q.push((HeapNode){d[v],v}); } } } } }; Dijksta solver; int main() { while(scanf("%d%d%d%d",&n,&k,&A,&B)!=EOF) { solver.init(n); for(int i=1;i<=k;i++) { int u,v;scanf("%d%d",&u,&v); solver.add(u,v,A); solver.add(v,u,A); } solver.solve(1); printf("%d\n",solver.d[n]); } return 0; }
UVA 10269 Adventure of Super Mario 最短路,布布扣,bubuko.com
UVA 10269 Adventure of Super Mario 最短路
原文:http://www.cnblogs.com/BMan/p/3647154.html