UVA 11765 Component Placement 網路流 新姿勢建圖,11765placement

來源:互聯網
上載者:User

UVA 11765 Component Placement 網路流 新姿勢建圖,11765placement

題目連結:點擊開啟連結

題意:

給定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 1152921504606846976struct 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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.