标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6802 Accepted Submission(s): 2124
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
struct edge{
int to,cap,rev;
};
vector<edge> G[1010];
map<string,int> mp;
int n,m,k,daymax,level[1010],iter[1010];
int p[1010],s[1010],e[1010],sum;
void add_edge(int u,int v,int cap)
{
G[u].push_back(edge{v,cap,G[v].size()});
G[v].push_back(edge{u,0,G[u].size()-1});
}
void bfs(int s)
{
queue<int> q;
q.push(s);
level[s]=1;
while(q.size())
{
int now=q.front();q.pop();
for(int i=0;i<G[now].size();i++)
if(G[now][i].cap>0)
{
edge e=G[now][i];
if(level[e.to]<0)
{
level[e.to]=level[now]+1;
q.push(e.to);
}
}
}
}
int dfs(int s,int t,int minn)
{
if(s==t)
return minn;
for(int &i=iter[s];i<G[s].size();i++)
{
edge &e=G[s][i];
if(level[e.to]>level[s]&&e.cap>0)
{
int k=dfs(e.to,t,min(minn,e.cap));
if(k>0)
{
e.cap-=k;
G[e.to][e.rev].cap+=k;
return k;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int ans=0,temp;
for(;;)
{
memset(level,-1,sizeof(level));
bfs(s);
if(level[t]<0)
return ans;
memset(iter,0,sizeof(iter));
while((temp=dfs(s,t,inf))>0)
ans+=temp;
}
return ans;
}
void build()
{
for(int i=0;i<=n+daymax+1;i++) G[i].clear();
for(int i=1;i<=n;i++)
add_edge(0,i,p[i]);
for(int i=n+1;i<=n+daymax;i++)
add_edge(i,n+daymax+1,m);
for(int i=1;i<=n;i++)
for(int j=1;j<=daymax;j++)
if(j>=s[i]&&j<=e[i])
add_edge(i,j+n,1);
}
int main()
{
int cas,kk=0;
scanf("%d",&cas);
while(cas--)
{
daymax=0;sum=0;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&p[i],&s[i],&e[i]);
if(e[i]>daymax) daymax=e[i];
sum+=p[i];
}
build();
printf("Case %d: ",++kk);
if(max_flow(0,n+daymax+1)==sum)
printf("Yes\n");//刚开始输出YES,wa了好久,剁手了,以后输出格式都直接粘贴!
else printf("No\n");
printf("\n");
}
return 0;
}
体会建图思想,刚开始我想的是建立一个天数与机器的二元组,然后向汇点连接一条容量为1的边,但是算下来就是会超时了,,因为点太多了,,,其实只要将天数向汇点连接容量为仪器数量的边就好了,这样就控制了仪器的使用数量,然后就是任务向仪器连边,跑跑最大流就可以了
标签:
原文地址:http://www.cnblogs.com/smilesundream/p/5512915.html