思路:
用long long
inf 要开大…….//By SiriusRen#include#include #include #include using namespace std;#define int long long#define N 1017#define M 999999#define inf 0x3f3f3f3f3f3f3f3fllint n,m,xx[M],yy[M],zz[M],first[N],next[M],v[M],w[M],tot,dis[N],vis[N],tmp,ans;struct Node{ int wei,now;}jy;bool operator < (Node a,Node b){ return a.wei>b.wei;}void Add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}struct Dijkstra{ void add(int x,int y,int z){Add(x,y,z),Add(y,x,z);} void solve(){ memset(dis,0x3f,sizeof(dis)),dis[1]=0; priority_queue pq;jy.now=1,pq.push(jy); while(!pq.empty()){ Node t=pq.top();pq.pop(); if(vis[t.now])continue; vis[t.now]=1; for(int i=first[t.now];~i;i=next[i]) if(dis[v[i]]>dis[t.now]+w[i]) dis[v[i]]=dis[t.now]+w[i], jy.now=v[i],jy.wei=dis[v[i]],pq.push(jy); } }}dijk;struct Dinic{ void add(int x,int y,int z){Add(x,y,z),Add(y,x,0);} bool tell(){ memset(vis,-1,sizeof(vis)),vis[1]=0; queue q;q.push(1); while(!q.empty()){ int t=q.front();q.pop(); for(int i=first[t];~i;i=next[i]) if(!~vis[v[i]]&&w[i]) vis[v[i]]=vis[t]+1,q.push(v[i]); }return ~vis[2*n]; } int zeng(int x,int y){ if(x==2*n)return y; int r=0; for(int i=first[x];~i;i=next[i]) if(vis[v[i]]==vis[x]+1&&w[i]){ int t=zeng(v[i],min(y-r,w[i])); w[i]-=t,w[i^1]+=t,r+=t; } if(!r)vis[x]=-1; return r; } void flow(){ while(tell())while(tmp=zeng(1,inf))ans+=tmp; printf("%lld\n",ans); }}dinic;void init(){ memset(first,-1,sizeof(first)),tot=0;}signed main(){ init(),scanf("%lld%lld",&n,&m); for(int i=1;i<=m;i++){ scanf("%lld%lld%lld",&xx[i],&yy[i],&zz[i]); dijk.add(xx[i],yy[i],zz[i]); } dijk.solve(),init(); for(int i=1;i<=m;i++){ if(dis[yy[i]]==dis[xx[i]]+zz[i])dinic.add(xx[i]+n,yy[i],inf); if(dis[xx[i]]==dis[yy[i]]+zz[i])dinic.add(yy[i]+n,xx[i],inf); } for(int i=1;i<=n;i++){ scanf("%lld",&tmp); if(i!=1&&i!=n)dinic.add(i,i+n,tmp); else dinic.add(i,i+n,inf); } dinic.flow();}