标签:hdu3572 task schedule dinic 最大流
2 4 3 1 3 5 1 1 4 2 3 7 3 5 9 2 2 2 1 3 1 2 2
Case 1: Yes Case 2: Yes
#include <cstdio> #include <cstring> #include <queue> #define SIZE 1100 #define INF 1000000000 using namespace std ; struct Edge{ int to , w , next ; }edge[SIZE*SIZE]; int n , m , start , ends , index = 0; int head[SIZE] , level[SIZE] , cur[SIZE]; bool visited[SIZE] ; bool bfs() { queue<int> que ; que.push(start) ; memset(level,-1,sizeof(level)) ; level[start] = 0 ; while(!que.empty()) { int pos = que.front() ; que.pop() ; for(int next = head[pos] ; next != -1 ; next = edge[next].next) { if(level[edge[next].to]<0 && edge[next].w>0) { level[edge[next].to] = level[pos]+1 ; que.push(edge[next].to) ; } } } return level[ends] != -1 ; } int min(int a , int b) { return a>b?b:a ; } int dfs(int pos , int flow) { int deta = 0 , tmp = 0; if(pos == ends) return flow ; for(int next = head[pos] ; next != -1 ; next = edge[next].next) { if(edge[next].w > 0 && level[pos] == level[edge[next].to]-1) { tmp = dfs(edge[next].to,min(flow-deta,edge[next].w)) ; if(tmp>0) { edge[next].w -= tmp ; edge[next^1].w += tmp ; deta += tmp ; if(deta == flow) break ; } else level[edge[next].to] = -1 ; //不加这个,,超时。。。 } } return deta ; } int dinic() { int ans = 0 , flow = 0 ; while(bfs()) { int deta = 0 ; ans += dfs(0,INF) ; } return ans ; } void add(int s , int d , int w) { edge[index].next = head[s] ; edge[index].to = d ; edge[index].w = w ; head[s] = index ++ ; edge[index].next = head[d] ; edge[index].to = s ; edge[index].w = 0 ; head[d] = index ++ ; } int main() { int t , c = 1 ; scanf("%d",&t) ; while(t--) { scanf("%d%d",&n,&m) ; memset(head,-1,sizeof(head)) ; index = 0 ; int sum = 0 , max = -1; start = 0; for(int i = 1 ; i <= n ; ++i) { int x , y , z ; scanf("%d%d%d",&x,&y,&z) ; sum += x ; max = max>z?max:z ; add(start,i,x) ; for(int j = y ; j <= z ; ++j) { add(i,j+n,1) ; } } ends = n+max+1 ; for(int i = 1 ; i <= max ; ++i) add(i+n,ends,m) ; int ans = dinic() ; printf("Case %d: ",c++) ; if(ans != sum) puts("No\n") ; else puts("Yes\n"); } return 0 ; }
hdu 3572 Task Schedule 最大流 Dinic算法,,卡时间。。建图非常有讲究
标签:hdu3572 task schedule dinic 最大流
原文地址:http://blog.csdn.net/lionel_d/article/details/45155637