标签:
有n个任务, m个机器, 每个任务有三个值, s,e, p, 表示, 这个任务必须在s天之后才能开始, 必须在e天之前结束, 并且必须要执行p天, 中间可以中断。 每个机器每天只能执行一个任务, 每个任务也只能被一个机器执行。
源点s和每个任务建边, 权值为p, 每个任务和s-e之间的这些天建边, 权值为1, 每一天和汇点建边, 权值为m。
模板题感觉没什么好说的啊......
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1061109567; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 const int maxn = 5e6+5; 20 int head[maxn*2], s, t, num, q[maxn*2], dis[10000]; 21 struct node 22 { 23 int to, nextt, c; 24 }e[maxn*2]; 25 void init() { 26 mem1(head); 27 num = 0; 28 } 29 void add(int u, int v, int c) { 30 e[num].to = v; e[num].nextt = head[u]; e[num].c = c; head[u] = num++; 31 e[num].to = u; e[num].nextt = head[v]; e[num].c = 0; head[v] = num++; 32 } 33 int bfs() { 34 int u, v, st = 0, ed = 0; 35 mem(dis); 36 dis[s] = 1; 37 q[ed++] = s; 38 while(st<ed) { 39 u = q[st++]; 40 for(int i = head[u]; ~i; i = e[i].nextt) { 41 v = e[i].to; 42 if(e[i].c&&!dis[v]) { 43 dis[v] = dis[u]+1; 44 if(v == t) 45 return 1; 46 q[ed++] = v; 47 } 48 } 49 } 50 return 0; 51 } 52 int dfs(int u, int limit) { 53 if(u == t) 54 return limit; 55 int cost = 0; 56 for(int i = head[u]; ~i; i = e[i].nextt) { 57 int v = e[i].to; 58 if(e[i].c&&dis[u] == dis[v]-1) { 59 int tmp = dfs(v, min(limit-cost, e[i].c)); 60 if(tmp>0) { 61 e[i].c -= tmp; 62 e[i^1].c += tmp; 63 cost += tmp; 64 if(cost == limit) 65 break; 66 } else { 67 dis[v] = -1; 68 } 69 } 70 } 71 return cost; 72 } 73 int dinic() { 74 int ans = 0; 75 while(bfs()) { 76 ans += dfs(s, inf); 77 } 78 return ans; 79 } 80 int main() 81 { 82 int T, m, n, x, y, w; 83 cin>>T; 84 for(int CASE = 1; CASE<=T; CASE++) { 85 scanf("%d%d", &n, &m); 86 init(); 87 int sum = 0, max_day = 0; 88 s = 0; 89 for(int i = 1; i<=n; i++) { 90 scanf("%d%d%d", &w, &x, &y); 91 add(s, i, w); 92 for(int j = x; j<=y; j++) { 93 add(i, j+n, 1); 94 } 95 max_day = max(max_day, y); 96 sum += w; 97 } 98 t = n+max_day+1; 99 for(int i = n+1; i<=n+max_day; i++) 100 add(i, t, m); 101 int ans = dinic(); 102 printf("Case %d: ", CASE); 103 if(ans == sum) { 104 cout<<"Yes"<<endl; 105 } else { 106 cout<<"No"<<endl; 107 } 108 cout<<endl; 109 } 110 }
标签:
原文地址:http://www.cnblogs.com/yohaha/p/5030891.html