bzoj 1001 [BeiJing2006]狼抓兔子——最小割转最短路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define ll long long using namespace std; const int N=2e6+5,M=6e6+5; int n,m,hd[N],xnt,to[M],nxt[M],w[M]; ll dis[N];bool vis[N]; priority_queue<pair<ll,int> > q; int rdn() { int ret=0;bool fx=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();} while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar(); return fx?ret:-ret; } void add(int x,int y,int z) { to[++xnt]=y;nxt[xnt]=hd[x];hd[x]=xnt;w[xnt]=z; } void dj() { memset(dis,0x3f,sizeof dis);dis[0]=0; q.push(make_pair(0,0)); while(q.size()) { int k=q.top().second;q.pop(); if(vis[k])continue; for(int i=hd[k],v;i;i=nxt[i]) if(dis[v=to[i]]>dis[k]+w[i]) dis[v]=dis[k]+w[i],q.push(make_pair(-dis[v],v)); } } int main() { n=rdn();m=rdn();int t=(n-1)*(m-1)*2+1,pl=m-1,ml=pl<<1; for(int i=1,d,lj=0;i<=n;i++,lj+=ml) { int x=lj+1; for(int j=1,y;j<m;j++,x++) { d=rdn();if(!lj)y=0;else y=x-pl; add(x>t?t:x,y,d);add(y,x>t?t:x,d);//not change x,for y } } for(int i=1,d,lj=m-1;i<n;i++,lj+=ml) { int x=lj+1; for(int j=1,y;j<=m;j++,x++) { d=rdn();if(x==lj+1)y=t;else y=x-m; if(x>lj+pl)x=0;//change x is ok for its the last x add(x,y,d);add(y,x,d); } } for(int i=1,d,lj=0;i<n;i++,lj+=ml) { int x=lj+1; for(int j=1,y;j<m;j++,x++) { d=rdn();y=x+pl; add(x,y,d);add(y,x,d); } } dj();printf("%lld\n",dis[t]); return 0; }