2014多校7(1006)hdu4940(有上下界的最大流)

Destroy Transportation system

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 114    Accepted Submission(s): 83


Problem Description
Tom is a commander, his task is destroying his enemy’s transportation system.

Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directed road. Each edge from node u to node v is associated with two values D and B, D is the cost to destroy/remove such edge, B is the cost to build an undirected edge between u and v.

His enemy can deliver supplies from city u to city v if and only if there is a directed path from u to v. At first they can deliver supplies from any city to any other cities. So the graph is a strongly-connected graph.

He will choose a non-empty proper subset of cities, let’s denote this set as S. Let’s denote the complement set of S as T. He will command his soldiers to destroy all the edges (u, v) that u belongs to set S and v belongs to set T. 

To destroy an edge, he must pay the related cost D. The total cost he will pay is X. You can use this formula to calculate X:

After that, all the edges from S to T are destroyed. In order to deliver huge number of supplies from S to T, his enemy will change all the remained directed edges (u, v) that u belongs to set T and v belongs to set S into undirected edges. (Surely, those edges exist because the original graph is strongly-connected)

To change an edge, they must remove the original directed edge at first, whose cost is D, then they have to build a new undirected edge, whose cost is B. The total cost they will pay is Y. You can use this formula to calculate Y:

At last, if Y>=X, Tom will achieve his goal. But Tom is so lazy that he is unwilling to take a cup of time to choose a set S to make Y>=X, he hope to choose set S randomly! So he asks you if there is a set S, such that Y<X. If such set exists, he will feel unhappy, because he must choose set S carefully, otherwise he will become very happy.
 

Input
There are multiply test cases.

The first line contains an integer T(T<=200), indicates the number of cases. 

For each test case, the first line has two numbers n and m. 

Next m lines describe each edge. Each line has four numbers u, v, D, B. 
(2=<n<=200, 2=<m<=5000, 1=<u, v<=n, 0=<D, B<=100000)

The meaning of all characters are described above. It is guaranteed that the input graph is strongly-connected.
 

Output
For each case, output "Case #X: " first, X is the case number starting from 1.If such set doesn’t exist, print “happy”, else print “unhappy”.
 

Sample Input
   
   
2 3 3 1 2 2 2 2 3 2 2 3 1 2 2 3 3 1 2 10 2 2 3 2 2 3 1 2 2
 

Sample Output
   
   
Case #1: happy Case #2: unhappy
Hint
In first sample, for any set S, X=2, Y=4. In second sample. S= {1}, T= {2, 3}, X=10, Y=4.

题意:将图的点分成S和T集合,要求对任意的分法X<=Y始终成立

思路:其实可以将X看成流量的下界,将Y看成流量的上界,可以将问题转化为求一个无源无汇的有上下界的最大流

这个建图可以这样来:
  
           对于任意的节点i,令flow=∑流入的下界流量-∑流出的下界流量
    
          1.flow>0  说明节点i一定要流入flow的流量,则从附加源点向i连边,流量为flow
          
          2.flow<0  说明节点i一定要流出flow的流量,则从i向附加汇点连边,流量为flow

         然后跑最大流,跑完之后检查附加源点的每条边是否满流即可,如果满流则满足

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; int n,m; #define INF ((1<<31)-1) struct node{ int u; int v; int cf; int f; }; int head[220]; int next[5500<<1]; node edge[5500<<1]; int level[220]; int q[220]; int d; int S,T; int ans; int in[220]; int out[220]; int stk[220]; int mini(int x,int y) { return x 
        
          0) { level[edge[i].v]=level[f]+1; q[rear++]=edge[i].v; if(edge[i].v==t)return 1; } } return 0; } int dfs(int u,int flow) { int i; if(u==T)return flow; int fl=0; int temp; for(i=head[u];i!=-1;i=next[i])if(level[edge[i].v]==level[u]+1&&(temp=edge[i].f-edge[i].cf)>0) { temp=dfs(edge[i].v,mini(temp,flow-fl)); edge[i].cf+=temp; edge[i^1].cf-=temp; fl+=temp; if(fl==flow)return flow; } if(!fl)level[u]=-1; return fl; } void dinic() { while(bfs(S,T))ans+=dfs(S,INF); } int main() { int t,i,j,u,v,b,dd,w,tt=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); d=0; memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); ans=0; S=0; T=n+1; for(i=0;i 
         
           out[i]){ stk[cnt++]=d; add(S,i,0,in[i]-out[i]); add(i,S,0,0); } else { add(i,T,0,out[i]-in[i]); add(T,i,0,0); } } dinic(); for(i=0;i 
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值