BZOJ P3931[CQOI2015]网络吞吐量

先跑一遍最短路

把最短路上的点建一个图,

然后需要拆点,因为权值在点上面

然后拆点限制流量

最后跑一边最大流就可以了

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int head[100003],exhead[100003],q[400003],d[400003];
struct map{
	long long f;
	int s,t,next;
}a[400003],exa[400003];
int edge,exedge,p;
inline void add(int s,int t,long long f){
     a[++edge].next=head[s];
     head[s]=edge;
     a[edge].s=s;
     a[edge].t=t;
     a[edge].f=f;
}
inline void exadd(int s,int t,long long f){
     exa[++exedge].next=exhead[s];
     exhead[s]=exedge;
     exa[exedge].s=s;
     exa[exedge].t=t;
     exa[exedge].f=f;
}
inline bool bfs(){
	int l=0,r=1;
	memset(q,0,sizeof(q));
	memset(d,-1,sizeof(d));
	q[r]=1;
	d[1]=0;
	while(l<r){
		int k=q[++l];
		for(int i=head[k];i;i=a[i].next){
			int to=a[i].t;
			if(a[i].f>0&&d[to]==-1){
				d[to]=d[k]+1;
				q[++r]=to;
			}
		}
	}
	if(d[p]>=0){
		return true;
	}else{
		return false;
	}
}
inline long long dfs(int now,long long f){
	if(now==p){
		return f;
	}
	long long t=f;
	for(int i=head[now];i;i=a[i].next){
		long long to=a[i].t;
		if(d[a[i].t]==d[now]+1&&a[i].f>0){
			long long xx=dfs(to,min(f,a[i].f));
			a[i].f-=xx;
			a[i^1].f+=xx;
			f-=xx;
		}
	}
	return t-f;
}
inline long long dinic(){
	long long fanhui=0;
	while(bfs()){
		fanhui+=dfs(1,(long long)1000000000*(long long)5000);
	}
	return fanhui;
}
long long dis[501];
bool v[501];
inline void spfa(){
	for(int i=1;i<=500;i++){
		dis[i]=(long long)1000000000*(long long)5000;
	}
	memset(v,false,sizeof(v));
	queue<int> Q;
	while(!Q.empty()){
		Q.pop();
	}
	Q.push(1);
	dis[1]=0;
	v[1]=true;
	while(!Q.empty()){
		int now=Q.front();Q.pop();
		v[now]=false;
		for(int i=exhead[now];i;i=exa[i].next){
			int t=exa[i].t;
			if(dis[t]>dis[now]+exa[i].f){
				dis[t]=dis[now]+exa[i].f;
				if(v[t]==false){
					Q.push(t);
					v[t]=true;
				}
			}
		}
	}
}
int b[100003];
int main(){
	edge=0;exedge=0;
	int n,m;cin>>n>>m;
	int s,t,x;
	for(int i=1;i<=m;i++){
		cin>>s>>t>>x;
		exadd(s,t,x);
		exadd(t,s,x);
	}
	spfa();
	p=n;
	for(int i=1;i<=n;i++){
		cin>>b[i];
		long long lim=b[i];
		if(i==1||i==n){
			lim=(long long)1000000000*(long long)5000;
		}
		add(i,i+p,lim);
		add(i+p,i,0);
	}
	for(int i=1;i<=exedge;i++){  
        if(dis[exa[i].s]+exa[i].f==dis[exa[i].t]){
            add(exa[i].s+p,exa[i].t,2100000000);
            add(exa[i].t,exa[i].s+p,0);
        }
    }
    p=n*2;
    long long ans=dinic();
    cout<<ans<<endl;
	return 0;
}
/*
in:
7 10
1 2 2
1 5 2
2 4 1
2 3 3
3 7 1
4 5 4
4 3 1
4 6 1
5 6 2
6 7 1
1
100
20
50
20
60
1

out:
70
*/ 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值