这道题是对有上下界网络流问题的第一次实践。这题不需要求最大流或最下流,只要判断是否存在可行流,如果存在输出这个可行流(根据题目中的这句话判断: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.)。不过话虽这么说,但是不知道OJ能不能真的判断所有可行的情况。
由于不需要求最大流或者最小流,所以构图然后运行一次最大流算法就好了(具体方法参考:http://blog.csdn.net/u011008379/article/details/38306477)。
不得不说,这题构图十分麻烦,这主要是和标准输入数据的格式有关,实在要处理太多情况,不过不是很难处理,有耐心就好。
此外,个人想提出一点对这道题的疑问:
for(i=a1;i<=a2;i++)
{
for(j=b1;j<=b2;j++)
{
if(ch=='=')
{
//if(up[i][j]<w||down[i][j]>w) return false;
up[i][j]=down[i][j]=w;
}else if(ch=='>') down[i][j]=max(down[i][j],w+1);
else if(ch=='<') up[i][j]=min(up[i][j],w-1);
}
}
被注释的这句话是用来判断题目给出的条件是否存在矛盾的,应该没有写错,可是如果把注释去掉,那么就会WA,注释掉才AC,不知为何。如果有哪位大牛发现我的错误,希望告诉我一下,非常感激!
最后感谢一下这篇文章:POJ2396 题解_forsona&Sona的POJ题解,因为这篇文章里提供了题目数据的链接(http://www.ida.liu.se/projects/progcontest/progsm/2003/),才让我发现这个错误的,真的很感谢。
代码(C++):
#include <cstdlib>
#include <iostream>
#include <queue>
#define MAX_N 30
#define MAX_M 300
#define INF 2000000000
using namespace std;
//#define LOCAL
struct Edge{
int from;
int to;
int cap;
int next;
} edge[MAX_N*MAX_M*4];
int head[MAX_M+MAX_N],up[MAX_M][MAX_N],down[MAX_M][MAX_N],n,m,c,level[MAX_M+MAX_N],sum;
bool get_info()
{
int count,r,p,w,a1,a2,b1,b2,i,j,k;
char ch;
scanf("%d",&count);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
up[i][j]=INF;
down[i][j]=0;
}
}
for(k=0;k<count;k++)
{
scanf("%d %d %c %d",&r,&p,&ch,&w);
a1=a2=r,b1=b2=p;
if(r==0) a1=1,a2=m;
if(p==0) b1=1,b2=n;
for(i=a1;i<=a2;i++)
{
for(j=b1;j<=b2;j++)
{
if(ch=='=')
{
//if(up[i][j]<w||down[i][j]>w) return false;
up[i][j]=down[i][j]=w;
}else if(ch=='>') down[i][j]=max(down[i][j],w+1);
else if(ch=='<') up[i][j]=min(up[i][j],w-1);
}
}
}
return true;
}
void add_edge(int u,int v,int cap)
{
edge[c].from=u;
edge[c].to=v;
edge[c].cap=cap;
edge[c].next=head[u];
head[u]=c;
c++;
edge[c].from=v;
edge[c].to=u;
edge[c].cap=0;
edge[c].next=head[v];
head[v]=c;
c++;
}
bool build_graph()
{
int i,j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(up[i][j]<down[i][j]) return false;
add_edge(i,j+200,up[i][j]-down[i][j]);
add_edge(i,n+200+3,down[i][j]);
add_edge(n+200+2,j+200,down[i][j]);
sum+=down[i][j];
}
}
add_edge(n+200+1,0,INF);
return true;
}
void bfs(int s)
{
int i,v,u;
queue<int> qi;
level[s]=0;
qi.push(s);
while(!qi.empty())
{
u=qi.front();
qi.pop();
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap>0&&level[v]==-1)
{
level[v]=level[u]+1;
qi.push(v);
}
}
}
}
int dfs(int u,int t,int f)
{
int i,v,d,res;
if(u==t) return f;
res=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(level[u]==level[v]-1&&edge[i].cap>0)
{
d=dfs(v,t,min(f-res,edge[i].cap));
if(d>0)
{
edge[i].cap-=d;
edge[i^1].cap+=d;
res+=d;
if(res==f) return res;
}
}
}
return res;
}
int dinic(int s,int t)
{
int flow,f;
flow=0;
while(1)
{
memset(level,-1,sizeof(level));
bfs(s);
if(level[t]==-1) return flow;
f=dfs(s,t,INF);
flow+=f;
}
}
int main(int argc, char *argv[]) //超级源点:0,超级汇点:n+200+1,临时源点:n+200+2,临时汇点:n+200+3
{ //expense标号:1~m site标号:200~n+200
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int i,j,t,s,f,u,v;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&m,&n);
c=0;
memset(head,-1,sizeof(head));
sum=0;
for(i=1;i<=m;i++)
{
scanf("%d",&s);
sum+=s;
add_edge(0,n+200+3,s);
add_edge(n+200+2,i,s);
}
for(i=1;i<=n;i++)
{
scanf("%d",&s);
sum+=s;
add_edge(i+200,n+200+3,s);
add_edge(n+200+2,n+200+1,s);
}
if(!get_info()||!build_graph())
{
printf("IMPOSSIBLE\n\n");
continue;
}
f=dinic(n+200+2,n+200+3);
if(f==sum)
{
for(i=(n+m)*4;i<c-2;i+=6)
{
u=edge[i].from;
v=edge[i].to;
v-=200;
down[u][v]+=edge[i+1].cap;
}
for(i=1;i<=m;i++)
{
printf("%d",down[i][1]);
for(j=2;j<=n;j++)
{
printf(" %d",down[i][j]);
}
printf("\n");
}
}else printf("IMPOSSIBLE\n");
if(t>0) printf("\n");
}
system("PAUSE");
return EXIT_SUCCESS;
}
题目( http://poj.org/problem?id=2396):
Time Limit: 3000MS | Memory Limit: 65536K | |||
Special Judge |
Description
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
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
Sample Input
2 2 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