poj 2396 zoj 1994 Budget(有源汇上下界的可行流)

2024-06-15 19:32

本文主要是介绍poj 2396 zoj 1994 Budget(有源汇上下界的可行流),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

思路:

无源汇 (附加源汇+最大解决)

有源汇 (附加(T,S)->无源汇)



Budget
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 6237
Accepted: 2367
Special Judge

Description

We are supposed to make a budget proposal for this multi-site competition. The budget proposal is a matrix where the rows represent different kinds of expenses and the columns represent different sites. We had a meeting about this, some time ago where we discussed the sums over different kinds of expenses and sums over different sites. There was also some talk about special constraints: someone mentioned that Computer Center would need at least 2000K Rials for food and someone from Sharif Authorities argued they wouldn't use more than 30000K Rials for T-shirts. Anyway, we are sure there was more; we will go and try to find some notes from that meeting. 

And, by the way, no one really reads budget proposals anyway, so we'll just have to make sure that it sums up properly and meets all constraints.

Input

The first line of the input contains an integer N, giving the number of test cases. The next line is empty, then, test cases follow: The first line of each test case contains two integers, m and n, giving the number of rows and columns (m <= 200, n <= 20). The second line contains m integers, giving the row sums of the matrix. The third line contains n integers, giving the column sums of the matrix. The fourth line contains an integer c (c < 1000) giving the number of constraints. The next c lines contain the constraints. There is an empty line after each test case. 

Each constraint consists of two integers r and q, specifying some entry (or entries) in the matrix (the upper left corner is 1 1 and 0 is interpreted as "ALL", i.e. 4 0 means all entries on the fourth row and 0 0 means the entire matrix), one element from the set {<, =, >} and one integer v, with the obvious interpretation. For instance, the constraint 1 2 > 5 means that the cell in the 1st row and 2nd column must have an entry strictly greater than 5, and the constraint 4 0 = 3 means that all elements in the fourth row should be equal to 3.

Output

For each case output a matrix of non-negative integers meeting the above constraints or the string "IMPOSSIBLE" if no legal solution exists. Put one empty line between matrices.

Sample Input

22 3 
8 10 
5 6 7 
4 
0 2 > 2 
2 1 = 3 
2 3 > 2 
2 3 < 5 2 2 
4 5 
6 7 
1 
1 1 > 10

Sample Output

2 3 3 
3 3 4 IMPOSSIBLE 

一道好题

首先要根据题目要求构造出一个容量网络  然后根据网络建图跑最大流 得到可行流

容量网络的构造方法:


如图  构造一个源点s(0) 一个汇点t(n+m+1) 源点与每行的节点建边 权值为每行的总和

每列的节点与汇点之间建边  权值为每列的总和

每行每列之间有上下界 根据题目的要求求出low[i][j] high[i][j] 

然后判断有木有不合理的情况 不合理的直接输出IMPOSSIBLE

之后开始建图求最大流

超级源点ss(n+m+2) 超级汇点tt(n+m+3)

每行的节点i与每列的节点j连接 i->j权值为high[i][j]-low[i][j]

容量网络里的每个点与ss或tt连接  out[i]为正与ss连接 权值为out[i]

out[i]为负与tt连接 权值为-out[i]

容量网络中s t之间也要建边 t->s权值INF

细节部门就是数据的读取处理出low[i][j] high[i][j] out[i]


<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#include <vector>#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define INF 0x3fffffff
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)using namespace std;void read(int &x)
{char ch;x=0;while(ch=getchar(),ch!=' '&&ch!='\n'){x=x*10+ch-'0';}
}struct Edge
{int from,to,cap,flow;bool operator <(const Edge e) const{if(e.from!=from)  return from<e.from;else return to<e.to;}Edge() {}Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};struct Dinic
{vector<Edge> edges;vector<int> G[MAXN];bool vis[MAXN];//BFS使用int d[MAXN];   //从起点到i的距离int cur[MAXN]; //当前弧下标int n,m,s,t,maxflow;   //节点数 边数(包括反向弧) 源点编号和弧点编号void init(int n){this->n=n;for(int i=0;i<=n;i++)G[i].clear();edges.clear();}void addedge(int from,int to,int cap){edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1);}bool bfs(){MEM(vis,0);MEM(d,-1);queue<int> q;q.push(s);d[s]=maxflow=0;vis[s]=1;while(!q.empty()){int u=q.front(); q.pop();int sz=G[u].size();for(int i=0;i<sz;i++){Edge e=edges[G[u][i]];if(!vis[e.to]&&e.cap>e.flow){d[e.to]=d[u]+1;vis[e.to]=1;q.push(e.to);}}}return vis[t];}int dfs(int u,int a){if(u==t||a==0)  return a;int sz=G[u].size();int flow=0,f;for(int &i=cur[u];i<sz;i++){Edge &e=edges[G[u][i]];if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){e.flow+=f;edges[G[u][i]^1].flow-=f;flow+=f;a-=f;if(a==0)  break;}}return flow;}int Maxflow(int s,int t){this->s=s; this->t=t;int flow=0;while(bfs()){MEM(cur,0);flow+=dfs(s,INF);}return flow;}
}Dic;int out[250];
int low[300][300];
int high[300][300];
int r[220],c[30];int main()
{
//    fread;int tc;scanf("%d",&tc);int cs=0;while(tc--){int n,m;scanf("%d%d",&n,&m);MEM(out,0);int line=0; int col=0;for(int i=1;i<=n;i++){int num;scanf("%d",&num);line+=num;out[0]-=num;out[i]+=num;}for(int j=1;j<=m;j++){int num;scanf("%d",&num);col+=num;out[n+m+1]+=num;out[j+n]-=num;}//out记录进入/出去 节点的流量  为正与源点建边  为负与汇点建边for(int i=0;i<300;i++)for(int j=0;j<300;j++){low[i][j]=0;high[i][j]=INF;}int ca;scanf("%d",&ca);int flag=0;while(ca--){int u,v,w;char ch;scanf("%d %d %c %d",&u,&v,&ch,&w);if(u==0&&v==0){for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){if(ch=='>') low[i][j+n]=max(low[i][j+n],w+1);if(ch=='<') high[i][j+n]=min(high[i][j+n],w-1);if(ch=='=') low[i][j+n]=high[i][j+n]=w;if(low[i][j+n]>high[i][j+n])flag=1;}}else if(u==0){for(int i=1;i<=n;i++){if(ch=='>') low[i][v+n]=max(low[i][v+n],w+1);if(ch=='<') high[i][v+n]=min(high[i][v+n],w-1);if(ch=='=') low[i][v+n]=high[i][v+n]=w;if(low[i][v+n]>high[i][v+n])flag=1;}}else if(v==0){for(int j=1;j<=m;j++){if(ch=='>') low[u][j+n]=max(low[u][j+n],w+1);if(ch=='<') high[u][j+n]=min(high[u][j+n],w-1);if(ch=='=') low[u][j+n]=high[u][j+n]=w;if(low[u][j+n]>high[u][j+n])flag=1;}}else{if(ch=='>') low[u][v+n]=max(low[u][v+n],w+1);if(ch=='<') high[u][v+n]=min(high[u][v+n],w-1);if(ch=='=') low[u][v+n]=high[u][v+n]=w;if(low[u][v+n]>high[u][v+n])flag=1;}}if(cs) puts("");cs=1;if(flag||line!=col){puts("IMPOSSIBLE");continue;}int s=n+m+2,t=n+m+3;Dic.init(t);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){out[i]-=low[i][j+n];out[j+n]+=low[i][j+n];Dic.addedge(i,j+n,high[i][j+n]-low[i][j+n]);}}Dic.addedge(n+m+1,0,INF);int sum=0;for(int i=0;i<=n+m;i++){if(out[i]>0){sum+=out[i];Dic.addedge(s,i,out[i]);}elseDic.addedge(i,t,-out[i]);}int mx=Dic.Maxflow(s,t);
//        cout<<mx<<endl;
//        cout<<"sum "<<sum<<endl;
//        int ffff=(Dic.Maxflow(s,t)==sum);
//        cout<<ffff<<endl;if(mx!=sum){
//            bug;puts("IMPOSSIBLE");continue;}
//        for(int i=0;i<n*m;i++)
//        {
//            int u=Dic.edges[i*2].from,v=Dic.edges[i*2].to;
//            cout<<"iii "<<i<<endl;
//            cout<<u<<" "<<v<<"  "<<Dic.edges[i*2].flow+low[u][v]<<endl;
//        }int id=0;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(j==1){printf("%d",Dic.edges[id*2].flow+low[i][j+n]);}elseprintf(" %d",Dic.edges[id*2].flow+low[i][j+n]);id++;}printf("\n");}
//        if(tc)
//            puts("");}return 0;
}
</span>







这篇关于poj 2396 zoj 1994 Budget(有源汇上下界的可行流)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064365

相关文章

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

poj 3159 (spfa差分约束最短路) poj 1201

poj 3159: 题意: 每次给出b比a多不多于c个糖果,求n最多比1多多少个糖果。 解析: 差分约束。 这个博客讲差分约束讲的比较好: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 套个spfa。 代码: #include <iostream>#include <cstdio>#i