天呐手贱打错了WA了N次好吧这不是重点
题意即题解
/************************************************************** Problem: 3931 User: BPM136 Language: C++ Result: Accepted Time:216 ms Memory:7984 kb ****************************************************************/ #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<cstdlib> #include<bitset> #define LL long long #define fo(i,a,b) for(int i=a;i<=b;i++) #define efo(i,x) for(int i=last[x];i!=0;i=e[i].next) using namespace std; inline LL read() { LL d=0,f=1;char s=getchar(); while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();} while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();} return f*d; } #define N 505 #define M 100005 #define inf 10000000000000LL LL a[N][N]; struct edg { int x,y; LL w; }ee[M]; struct edge { int y,next; LL f; }e[M*2]; int last[N*2],ne=1; int n,m; void add(int x,int y,LL f) { e[++ne].y=y;e[ne].f=f;e[ne].next=last[x];last[x]=ne; } void add2(int x,int y,LL f) { add(x,y,f);add(y,x,0); } void init1() { n=read(),m=read(); fo(i,1,m) { int x=read(),y=read(); LL w=read(); // add2(x,y,w); ee[i].x=x;ee[i].y=y;ee[i].w=w; if(a[x][y]==0) { a[x][y]=w; a[y][x]=w; } else { a[x][y]=min(a[x][y],w); a[y][x]=a[x][y]; } } } LL dis[N]; bitset<N>v; void dijkstra() { v.reset(); fo(i,0,N-1)dis[i]=inf; dis[1]=0; while(1) { LL mi=inf; int u=0; fo(i,1,n) if(v[i]==0&&dis[i]<mi) { mi=dis[i]; u=i; } if(u==0)return; v[u]=1; fo(i,1,n) if(v[i]==0&&a[u][i]) { dis[i]=min(dis[u]+a[u][i],dis[i]); } } } int q[N*9]; void spfa() { fo(i,0,N-1)dis[i]=inf; v.reset(); int h=0,t=1; dis[1]=0;q[1]=1;v[1]=1; while(h<t) { int now=q[++h]; efo(i,now) if(dis[now]+e[i].f<dis[e[i].y]&&e[i].f!=0) { dis[e[i].y]=dis[now]+e[i].f; if(v[e[i].y]==0) { q[++t]=e[i].y; v[e[i].y]=1; } } v[now]=0; } } void build() { memset(last,0,sizeof(last));ne=1; fo(i,1,m) { int x=ee[i].x,y=ee[i].y; LL w=ee[i].w; if(dis[x]+w==dis[y]) { add2(x+n,y,inf); } if(dis[y]+w==dis[x]) { add2(y+n,x,inf); } } fo(i,1,n) { LL f=read(); if(i!=1&&i!=n)add2(i,i+n,f); else add2(i,i+n,inf); } } int hi[N*2]; bool bfs(int s,int tt) { memset(hi,0,sizeof(hi)); int h=0,t=1; q[1]=s;hi[s]=1; while(h<t) { int now=q[++h]; if(now==tt)return 1; efo(i,now) if(e[i].f&&hi[e[i].y]==0) { hi[e[i].y]=hi[now]+1; q[++t]=e[i].y; } } return 0; } LL dfs(int s,LL maxf,int tt) { if(s==tt)return maxf; LL f,ret=0; efo(i,s) if(e[i].f&&hi[e[i].y]==hi[s]+1) { f=dfs(e[i].y,min(e[i].f,maxf-ret),tt); ret+=f; e[i].f-=f; e[i^1].f+=f; if(ret==maxf)return ret; } return ret; } LL dinic() { LL ret=0; while(bfs(1,n+n))ret+=dfs(1,inf,n+n); return ret; } int main() { memset(a,0,sizeof(a)); init1(); // spfa(); dijkstra(); // fo(i,1,n)cout<<dis[i]<<' ';cout<<endl; build(); printf("%lld\n",dinic()); return 0; }