hdu4940 有上下界的无源可行流判断
生活随笔
收集整理的這篇文章主要介紹了
hdu4940 有上下界的无源可行流判断
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題意:
? ? ? 給你一個強(qiáng)連通圖,然后問你是否可以找到任意滿足條件的集合S,S是非空集合,T是S的補(bǔ)集,滿足sum(D[i ,j]) <= sum(D[j,i] + B[j,i]) i屬于S集合,j屬于T集合(其實也就暗示了i,j是S,T的割邊)。
思路:
? ? ? 給你一個強(qiáng)連通圖,然后問你是否可以找到任意滿足條件的集合S,S是非空集合,T是S的補(bǔ)集,滿足sum(D[i ,j]) <= sum(D[j,i] + B[j,i]) i屬于S集合,j屬于T集合(其實也就暗示了i,j是S,T的割邊)。
思路:
? ? ? ?無源匯上下流可行流判斷問題,首先題目給的圖是一個強(qiáng)連通圖,為了方便理解,我們假設(shè)這個圖只有兩個點,a,b,那么肯定也只有兩條邊,a->b ,b->a,那么我們可以直接建邊a->b(下界 D 上界 B + D) b->a(下界 D 上界 B + D)這樣跑一遍上下流之后如果存在可行流,那么就存在一個a,b之間的循環(huán)流(循環(huán)流的大小我們不用關(guān)心,我們只關(guān)心是否存在),那么就會有這樣的結(jié)論,a->b的D(下限)一定小于等于b->a 的D+B(上限),同時 b->a的D(下限) 一定小于等于a->b的 D+B(上限),所以無論是a,還是b都可以充當(dāng)S集合。so如果整個圖中任意兩個集合都這樣就顯然可以滿足題意了。
#include<stdio.h> #include<string.h> #include<queue>#define N_node 220 #define N_edge 33000 #define INF 1000000000 using namespace std;typedef struct {int to ,next ,cost; }STAR;typedef struct {int x ,t; }DEP;STAR E[N_edge]; DEP xin ,tou; int list[N_node] ,listt[N_node] ,tot; int deep[N_node] ,sum_must;void add(int a ,int b ,int c) {E[++tot].to = b;E[tot].cost = c;E[tot].next = list[a];list[a] = tot;E[++tot].to = a;E[tot].cost = 0;E[tot].next = list[b];list[b] = tot; }void ADD(int a ,int b ,int c ,int d ,int ss ,int tt) {add(a ,b ,d - c);add(a ,tt ,c);add(ss ,b ,c);sum_must += c; }int minn(int x ,int y) {return x < y ? x : y; }bool BFS_Deep(int s ,int t ,int n) {xin.x = s ,xin.t = 0;queue<DEP>q;q.push(xin);memset(deep ,255 ,sizeof(deep));deep[s] = 0;while(!q.empty()){tou = q.front();q.pop();for(int k = list[tou.x] ;k ;k = E[k].next){xin.x = E[k].to;xin.t = tou.t + 1;if(deep[xin.x] != -1 || !E[k].cost)continue;deep[xin.x] = xin.t;q.push(xin);}}for(int i = 0 ;i <= n ;i ++)listt[i] = list[i];return deep[t] != -1; }int DFS_Flow(int s ,int t ,int flow) {if(s == t) return flow;int nowflow = 0;for(int k = listt[s] ;k ;k = E[k].next){listt[s] = k;int to = E[k].to;int c = E[k].cost;if(deep[to] != deep[s] + 1 || !c)continue;int tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow));nowflow += tmp;E[k].cost -= tmp;E[k^1].cost += tmp;if(nowflow == flow)break;}if(!nowflow) deep[s] = 0;return nowflow; }int DINIC(int s ,int t ,int n) {int ans = 0;while(BFS_Deep(s ,t ,n)){ans += DFS_Flow(s ,t ,INF);}return ans; }int main () {int t ,n ,m ,i ,a ,b ,c ,d ,cas = 1;scanf("%d" ,&t);while(t--){scanf("%d %d" ,&n ,&m);int ss = 0 ,tt = n + 1;memset(list ,0 ,sizeof(list));tot = 1 ,sum_must = 0;for(i = 1 ;i <= m ;i ++){scanf("%d %d %d %d" ,&a ,&b ,&c ,&d);ADD(a ,b ,c ,c + d ,ss ,tt);}printf("Case #%d: " ,cas ++);sum_must == DINIC(ss ,tt ,tt) ? puts("happy") : puts("unhappy");}return 0; }
總結(jié)
以上是生活随笔為你收集整理的hdu4940 有上下界的无源可行流判断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu4515 小模拟
- 下一篇: POJ 2396 构造矩阵(上下流)