首页 > 代码库 > UVA 11765 Component Placement 网络流 新姿势建图

UVA 11765 Component Placement 网络流 新姿势建图

题目链接:点击打开链接

题意:

给定n个物品, m个约束条件

把n个物品分到2个集合里

下面第一行表示i物品分到第一个集合里的花费

第二行表示分到第二个集合里的花费

第三行表示分物品的限制(1表示只能分到第一个集合,-1表示只能分到第二个集合,0无限制)

下面m行给出约束条件

u v cost 表示u v 两点必须能互相沟通,若两点已经在同一集合则花费为0 ,若不在同一集合则花费增加cost


问满足m个约束条件下的最小花费


思路:

首先感觉是网络流,==建图比较难想

用流量表示费用


1、若i点放入第一个集合,则向源点连一条流量为a[i]的边, 若放入第二个集合则向汇点连一条流量为b[i]的边

如此便可以使得一个点放入任意一个集合花费最小


2、对于约束条件 则向 u, v两点连一条无向的边,流量为cost

若u, v两点被第三行约束在同一集合,则这条边加了也不能增加流量,也就是费用增加0

若被约束在不同集合则相当于必然增加了cost

Orz

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

#define ll long long

#define N 100040
#define M 205000
#define inf 107374182
#define inf64 1152921504606846976
struct Edge{
	ll from, to, cap, nex;
}edge[M*2];//注意这个一定要够大 不然会re 还有反向弧

ll head[N], edgenum;
void add(ll u, ll v, ll cap, ll rw = 0){ //如果是有向边则:add(u,v,cap); 如果是无向边则:add(u,v,cap,cap);
	Edge E = { u, v, cap, head[u]};
	edge[ edgenum ] = E;
	head[u] = edgenum ++;

	Edge E2= { v, u, rw,  head[v]};
	edge[ edgenum ] = E2;
	head[v] = edgenum ++;
}
ll sign[N];
bool BFS(ll from, ll to){
	memset(sign, -1, sizeof(sign));
	sign[from] = 0;

	queue<ll>q;
	q.push(from);
	while( !q.empty() ){
		ll u = q.front(); q.pop();
		for(ll i = head[u]; i!=-1; i = edge[i].nex)
		{
			ll v = edge[i].to;
			if(sign[v]==-1 && edge[i].cap)
			{
				sign[v] = sign[u] + 1, q.push(v);
				if(sign[to] != -1)return true;
			}
		}
	}
	return false;
}
ll Stack[N], top, cur[N];
ll Dinic(ll from, ll to){
	ll ans = 0;
	while( BFS(from, to) )
	{
		memcpy(cur, head, sizeof(head));
		ll u = from;      top = 0;
		while(1)
		{
			if(u == to)
			{
				ll flow = inf, loc;//loc 表示 Stack 中 cap 最小的边
				for(ll i = 0; i < top; i++)
					if(flow > edge[ Stack[i] ].cap)
					{
						flow = edge[Stack[i]].cap;
						loc = i;
					}

					for(ll i = 0; i < top; i++)
					{
						edge[ Stack[i] ].cap -= flow;
						edge[Stack[i]^1].cap += flow;
					}
					ans += flow;
					top = loc;
					u = edge[Stack[top]].from;
			}
			for(ll i = cur[u]; i!=-1; cur[u] = i = edge[i].nex)//cur[u] 表示u所在能增广的边的下标
				if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break;
			if(cur[u] != -1)
			{
				Stack[top++] = cur[u];
				u = edge[ cur[u] ].to;
			}
			else
			{
				if( top == 0 )break;
				sign[u] = -1;
				u = edge[ Stack[--top] ].from;
			}
		}
	}
	return ans;
}
void init(){memset(head,-1,sizeof head);edgenum = 0;}

ll n, m;
ll a[N], b[N], c[N];
void input(){
    ll u, v, cos;
    cin>>n>>m;
    init();
    for(ll i = 1; i <= n; i++)scanf("%lld", &a[i]);
    for(ll i = 1; i <= n; i++)scanf("%lld", &b[i]);
    for(ll i = 1; i <= n; i++)scanf("%lld", &c[i]);
}
void work(){
    ll from = 0, to = n+10, u, v, cos;
    for(ll i = 1; i <= n; i++)
    {
        if(c[i]==1)
            add(from, i, a[i]), add(i, to, inf);
        else if(c[i]==-1)
            add(from, i, inf), add(i, to, b[i]);
        else add(from, i, a[i]), add(i, to, b[i]);
    }
    while(m--)
    {
        scanf("%lld %lld %lld",&u,&v,&cos);
        add(u, v, cos); add(v, u, cos);
    }
    cout<<Dinic(from, to)<<endl;
}
int main(){
    int T; cin>>T;
    while(T--){
        input();
        work();
    }
    return 0;
}