标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + ‘0‘);
putchar(‘\n‘);
}
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=210;
const double eps=1e-12;
int n,m,l,num[maxn],a[maxn][maxn],v[maxn],p[maxn];
int cap[2*maxn][2*maxn],path[2*maxn],minflow[2*maxn],s,e;
queue<int>qu;
struct node
{
int va,t,id;
}po[210];
int cmp(node x,node y)
{
return x.t<y.t;
}
int bfs()
{
mst(path,-1);
qu.push(s);
path[s]=0;minflow[s]=inf;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
for(int i=1;i<=e;i++)
{
if(path[i]==-1&&cap[fr][i])
{
minflow[i]=min(minflow[fr],cap[fr][i]);
path[i]=fr;
qu.push(i);
}
}
}
if(path[e]==-1)return 0;
return minflow[e];
}
int maxflow(int x)
{
mst(cap,0);
int sum=0;
For(i,1,m)
{
if(po[a[i][num[i]]].t<=po[x].t)
{
sum+=v[i];
cap[s][i]=v[i];
For(j,1,num[i])cap[i][m+a[i][j]]=inf;
}
}
for(int i=1;i<=x;i++)
{
cap[i+m][e]=po[i].va;
}
int ans=0;
while(bfs())
{
int temp=minflow[e];
int cur=e;
while(cur!=s)
{
int fa=path[cur];
cap[fa][cur]-=temp;
cap[cur][fa]+=temp;
cur=fa;
}
ans+=temp;
}
//cout<<sum<<" "<<ans<<" &&&&"<<endl;
if(sum-ans>=l)
{
printf("%d %d\n",po[x].t,sum-ans);
return 1;
}
return 0;
}
int solve()
{
s=0;e=n+m+1;
int flag=1;
For(i,1,n)
{
if(i!=n&&po[i].t==po[i+1].t)continue;
else
{
if(maxflow(i)){flag=0;break;}
}
}
if(flag)printf("impossible\n");
}
int main()
{
int t,Case=0;
read(t);
while(t--)
{
printf("Case #%d: ",++Case);
read(n);read(m);read(l);
For(i,1,n)
{
read(po[i].va);read(po[i].t);
po[i].id=i;
}
sort(po+1,po+n+1,cmp);
For(i,1,n)p[po[i].id]=i;
For(i,1,m)
{
read(v[i]);read(num[i]);
For(j,1,num[i])
{
read(a[i][j]);
a[i][j]=p[a[i][j]];
}
sort(a[i]+1,a[i]+num[i]+1);
}
solve();
}
return 0;
}
hdu-5855 Less Time, More profit(最大权闭合子图)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5777786.html