时间来不及了,先贴代码吧!有时间再写。
好苦逼啊,WA了若干次,还有一次RE,一次TLE。
虽然主要运用的算法和资料都由师兄提供了。还是太弱了,太天真了。
1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<cstring> 5 #define maxn 2100009 6 #define rep(i,j,k) for(int i = j; i <= k; i++) 7 using namespace std; 8 9 int read() 10 { 11 int s = 0, t = 1; char c = getchar(); 12 while( !isdigit(c) ) { 13 if( c == ‘-‘ ) t = -1; c = getchar(); 14 } 15 while( isdigit(c) ){ 16 s = s * 10 + c - ‘0‘; c = getchar(); 17 } 18 return s * t; 19 } 20 21 int d[maxn], n, m, sx, tx; 22 bool done[maxn] = {0}; 23 24 struct node{ 25 int d, u; 26 bool operator < (const node& rhs) const{ 27 return d > rhs.d; 28 } 29 }; 30 31 struct edge{ 32 int to, key; 33 edge* next; 34 }; 35 36 edge *pt, edges[maxn*3], *head[maxn]; 37 38 void add_edge(int x,int y,int val){ 39 pt->to = y;pt->key = val; 40 pt->next = head[x]; 41 head[x] = pt++; 42 pt->to = x, pt->key= val; 43 pt->next = head[y]; 44 head[y] = pt++; 45 } 46 priority_queue<node> Q; 47 48 void dijkstra() 49 { 50 memset(d,127,sizeof(d)); 51 d[sx] = 0; 52 Q.push((node){0,sx}); 53 while( !Q.empty() ){ 54 node x = Q.top(); Q.pop(); 55 int u = x.u; 56 if( done[u] ) continue; 57 done[u] = 1; 58 for( edge*i = head[u]; i ; i = i->next ){ 59 int y = i->to, key = i->key; 60 if( d[y] > d[u]+key ) { 61 d[y] = d[u]+key; 62 Q.push((node){d[y],y}); 63 } 64 } 65 } 66 } 67 68 int main() 69 { 70 //freopen("out.txt","w",stdout); 71 pt = edges; 72 n = read(), m = read(); 73 sx = 0, tx = (n-1)*(m-1) * 2+ 1; 74 rep(i,0,n-1) { 75 int xx = (2*m-2)*i; 76 int xy = (2*m-2)*(i-1); 77 rep(j,1,m-1){ 78 int x = read(); 79 if( !i ) { 80 add_edge(xx+2*j,tx,x); 81 //cout<<tx<<" "<<xx+2*j+1<<" "<<x<<endl; 82 } 83 else if( i == n-1 ) { 84 add_edge(sx,xy+2*j-1,x); 85 //cout<<xy+2*j<<" "<<sx<<" "<<x<<endl; 86 } 87 else{ 88 add_edge(xx+2*j,xy+2*j-1,x); 89 //cout<<xy+2*j<<" "<<xx+2*j+1<<" "<<x<<endl; 90 } 91 } 92 } 93 rep(i,0,n-2){ 94 int xx = 2*(m-1)*(i); 95 rep(j,1,m){ 96 int x = read(); 97 if( j == 1 ){ 98 add_edge(sx,xx+2*j-1,x); 99 //cout<<sx<<" "<<xx<<" "<<x<<endl; 100 } 101 else if( j == m ){ 102 add_edge(tx,xx+2*j-2,x); 103 //cout<<tx<<" "<<xx+2*j-1<<" "<<x<<endl; 104 } 105 else { 106 add_edge(xx+2*j-2,xx+2*j-1,x); 107 //cout<<xx+2*j<<" "<<xx+2*j-1<<" "<<x<<endl; 108 } 109 } 110 } 111 rep(i,0,n-2){ 112 int xx = 2*(m-1)*(i); 113 rep(j,1,m-1){ 114 int x = read(); 115 add_edge(xx+2*j,xx+2*j-1,x); 116 //cout<<xx+2*j<<" "<<xx+2*j+1<<" "<<endl; 117 } 118 } 119 dijkstra(); 120 cout<<d[tx]<<endl; 121 return 0; 122 }
时间: 2024-10-24 20:04:20