hdu3572 给定n个任务的初末时间,每个任务要p[i]天数完成,问能否按规定完成所有任务 :经典建图/网络流
设定源点st,汇点ed
将任务和每天当作两部分点建图
源点连向每个任务,流量为该任务所需时间
每个任务连向他可以可以做的每一天,流量为1
每天连向汇点,流量为m,表示一天最多做m个任务
==
跑一遍最大流,如果最大流==所有任务需要天数之和即Yes,否则不能完成任务
熟悉各种模型!
1 #pragma comment(linker,"/STACK:16777216") 2 #include<stdio.h> 3 #include<string.h> 4 #include<queue> 5 #include<vector> 6 #include<algorithm> 7 #define maxn 150005 8 #define maxm 200005 9 #define inf 0x3f3f3f3f 10 using namespace std; 11 struct Edge{ 12 int from,to,cap,flow; 13 }; 14 struct dian{ 15 int x,y; 16 }; 17 struct dinic{ 18 int s,t,n; 19 vector<Edge>edges; 20 vector<int>G[maxn]; 21 bool vis[maxn]; 22 int cur[maxn],d[maxn]; 23 void init(int _s,int _t,int _n){ 24 s=_s; t=_t; n=_n; 25 edges.clear(); 26 for (int i=0;i<=_n;i++) G[i].clear(); 27 } 28 void add(int from,int to,int cap){ 29 Edge e; 30 e.from=from; e.to=to; e.cap=cap; e.flow=0; 31 edges.push_back(e); 32 e.from=to; e.to=from; e.cap=0; e.flow=0; 33 edges.push_back(e); 34 int m=edges.size(); 35 G[from].push_back(m-2); G[to].push_back(m-1); 36 } 37 bool bfs(){ 38 memset(vis,0,sizeof(vis)); 39 queue<int>q; 40 q.push(s); d[s]=0; vis[s]=1; 41 while (!q.empty()){ 42 int x=q.front(); q.pop(); 43 for (int i=0;i<G[x].size();i++){ 44 Edge& e=edges[G[x][i]]; 45 if (!vis[e.to]&&e.cap>e.flow){ 46 vis[e.to]=1; 47 d[e.to]=d[x]+1; 48 q.push(e.to); 49 } 50 } 51 } 52 return vis[t]; 53 } 54 int dfs(int x,int a){ 55 if (x==t||a==0) return a; 56 int flow=0,f; 57 for (int& i=cur[x];i<G[x].size();i++){ 58 Edge& e=edges[G[x][i]]; 59 if (d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){ 60 e.flow+=f; 61 edges[G[x][i]^1].flow-=f; 62 flow+=f; 63 a-=f; 64 if (a==0) break; 65 } 66 } 67 return flow; 68 } 69 int maxflow(){ 70 int flow=0; 71 while (bfs()){ 72 memset(cur,0,sizeof(cur)); 73 flow+=dfs(s,inf); 74 } 75 return flow; 76 } 77 }g; 78 int main() 79 { 80 int T,t,st,ed,s,e,p,i,j,ans,n,m; 81 scanf("%d",&T); 82 for (t=1;t<=T;t++){ 83 st=0; ed=1001; ans=0; 84 scanf("%d%d",&n,&m); 85 g.init(st,ed,ed+1); 86 for (i=1;i<=n;i++){ 87 scanf("%d%d%d",&p,&s,&e); 88 ans+=p; 89 g.add(st,i,p); 90 for (j=s;j<=e;j++) 91 g.add(i,n+j,1); 92 } 93 for (i=n+1;i<ed;i++) 94 g.add(i,ed,m); 95 printf("Case %d: ",t); 96 if (g.maxflow()==ans) printf("Yes\n\n"); 97 else printf("No\n\n"); 98 } 99 return 0; 100 }