HDU-4725 The Shortest Path in Nya Graph 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725
如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n+i。n+i节点向 i 层的所有连边,权值为0。i 层的所有点向2*n+i节点连边,权值为0。然后每层直接建立边就可以了,即2*n+i-1向n+i连边,权值为c,2*n+i向n+i-1连边,权值为c。3*n个点,最多有有9*n条边。。
1 //STATUS:C++_AC_730MS_14340KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,102400000") 25 //using namespace __gnu_cxx; 26 //define 27 #define pii pair<int,int> 28 #define mem(a,b) memset(a,b,sizeof(a)) 29 #define lson l,mid,rt<<1 30 #define rson mid+1,r,rt<<1|1 31 #define PI acos(-1.0) 32 //typedef 33 typedef __int64 LL; 34 typedef unsigned __int64 ULL; 35 //const 36 const int N=300010; 37 const int INF=0x3f3f3f3f; 38 const int MOD=1000000007,STA=8000010; 39 const LL LNF=1LL<<60; 40 const double EPS=1e-8; 41 const double OO=1e60; 42 const int dx[4]={-1,0,1,0}; 43 const int dy[4]={0,1,0,-1}; 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 45 //Daily Use ... 46 inline int sign(double x){return (x>EPS)-(x<-EPS);} 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 50 template<class T> inline T Min(T a,T b){return a<b?a:b;} 51 template<class T> inline T Max(T a,T b){return a>b?a:b;} 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 56 //End 57 58 struct Edge{ 59 int u,v,w; 60 }e[3*N]; 61 int first[N],next[3*N]; 62 LL d[N]; 63 int S,T,n,m,c,mt; 64 65 void adde(int a,int b,int c) 66 { 67 e[mt].u=a,e[mt].v=b;e[mt].w=c; 68 next[mt]=first[a],first[a]=mt++; 69 } 70 #define pli pair<LL,int> 71 LL dijkstra(int s) 72 { 73 int i,j,u,v,x; 74 pli t; 75 priority_queue<pli,vector<pli>,greater<pli> > q; 76 for(i=1;i<=3*n;i++)d[i]=LNF; 77 d[s]=0; 78 q.push(make_pair(d[s],s)); 79 while(!q.empty()){ 80 t=q.top();q.pop(); 81 u=t.second; 82 if(t.first!=d[u])continue; 83 for(i=first[u];i!=-1;i=next[i]){ 84 if(d[u]+e[i].w<d[e[i].v]){ 85 d[e[i].v]=d[u]+e[i].w; 86 q.push(make_pair(d[e[i].v],e[i].v)); 87 } 88 } 89 } 90 return d[T]; 91 } 92 93 int main(){ 94 // freopen("in.txt","r",stdin); 95 int Ca,i,j,k,a,b,w,ca=1; 96 scanf("%d",&Ca); 97 while(Ca--) 98 { 99 scanf("%d%d%d",&n,&m,&c); 100 S=1,T=n; 101 mem(first,-1);mt=0; 102 for(i=1;i<=n;i++){ 103 scanf("%d",&a); 104 adde(n+a,i,0); 105 adde(i,(n<<1)+a,0); 106 } 107 for(i=2;i<=n;i++){ 108 adde((n<<1)+i-1,n+i,c); 109 adde((n<<1)+i,n+i-1,c); 110 } 111 for(i=0;i<m;i++){ 112 scanf("%d%d%d",&a,&b,&w); 113 adde(a,b,w); 114 adde(b,a,w); 115 } 116 dijkstra(S); 117 if(d[T]==LNF)d[T]=-1; 118 119 printf("Case #%d: %I64d\n",ca++,d[T]); 120 } 121 return 0; 122 }