标签:des style blog http io color ar os java
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940


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
Case #1: happy Case #2: unhappyHintIn first sample, for any set S, X=2, Y=4. In second sample. S= {1}, T= {2, 3}, X=10, Y=4.
官方题解:http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html
寻找是否存在能单独作为S集合的点!
代码例如以下:
//#pragma warning (disable:4786)
#include <cstdio>
#include <cstring>
typedef long long LL;
#define N 1017
LL X[N], Y[N];
void init()
{
memset(X, 0, sizeof(X));
memset(Y, 0, sizeof(Y));
}
int main()
{
int n, m;
int u, v, D, B;
int t;
int cas = 0;
int flag = 0;
int i;
scanf("%d", &t);
while(t--)
{
init();
flag = 0;
scanf("%d%d", &n,&m);
for(i=1; i<=m; i++)
{
scanf("%d%d%d%d", &u, &v, &D, &B);
X[u]+=D;
Y[v]+=(D+B);
}
for(i = 1; i <= n; i++)
{
if(Y[i] < X[i])
{
flag = 1;
break;
}
}
if(flag)
printf("Case #%d: unhappy\n",++cas);
else
printf("Case #%d: happy\n",++cas);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 209
#define maxm 20000
#define INF 1e9
using namespace std;
struct Edge
{
int v,cap,next;
}edge[maxm];
int n,tot,src,des;
int head[maxn],h[maxn],gap[maxn],B[maxn];
void addedge(int u,int v,int cap)
{
edge[tot].v=v;
edge[tot].cap=cap;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].v=u;
edge[tot].cap=0;
edge[tot].next=head[v];
head[v]=tot++;
}
int dfs(int u,int cap)
{
if(u==des)return cap;
int minh=n-1;
int lv=cap,d;
for(int e=head[u];e!=-1;e=edge[e].next)
{
int v=edge[e].v;
if(edge[e].cap>0)
{
if(h[v]+1==h[u])
{
d=min(lv,edge[e].cap);
d=dfs(v,d);
edge[e].cap-=d;
edge[e^1].cap+=d;
lv-=d;
if(h[src]>=n)return cap-lv;
if(lv==0)
break;
}
minh=min(minh,h[v]);
}
}
if(lv==cap)
{
--gap[h[u]];
if(gap[h[u]]==0)
h[src]=n;
h[u]=minh+1;
++gap[h[u]];
}
return cap-lv;
}
int sap()
{
int flow=0;
memset(gap,0,sizeof(gap));
memset(h,0,sizeof(h));
gap[0]=n;
while(h[src]<n)flow+=dfs(src,INF);
return flow;
}
int main()
{
int N,M;
int cot=1;
int CAS;
scanf("%d", &CAS);
while(CAS--)
{
scanf("%d%d",&N,&M);
memset(head,-1,sizeof(head));
memset(B,0,sizeof(B));
tot=0;src=0;des=N+1;n=des+1;
for(int i=1;i<=M;i++)
{
int u,v,b,l;
scanf("%d%d%d%d",&u,&v,&b,&l);
addedge(u,v,l);
B[v]+=b;B[u]-=b;
}
int sum=0;
for(int i=1;i<=N;i++)
{
if(B[i]>0)
{
addedge(src,i,B[i]);
sum+=B[i];
}
else if(B[i]<0)
{
addedge(i,des,-B[i]);
}
}
int flow=sap();
if(flow==sum)
{
printf("Case #%d: happy\n",cot++);
}
else
{
printf("Case #%d: unhappy\n",cot++);
}
}
return 0;
}
hdu 4940 Destroy Transportation system(水过)
标签:des style blog http io color ar os java
原文地址:http://www.cnblogs.com/bhlsheji/p/4076290.html