标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2883
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #define inf 0x7fffffff 10 using namespace std; 11 const int maxn=600+10; 12 const int M = 999999; 13 14 int n,m,from,to; 15 int d[maxn]; 16 struct node 17 { 18 int v,flow; 19 int next; 20 }edge[M*2]; 21 int head[maxn],edgenum; 22 23 void add(int u,int v,int flow) 24 { 25 edge[edgenum].v=v ;edge[edgenum].flow=flow ; 26 edge[edgenum].next=head[u]; 27 head[u]=edgenum++; 28 29 edge[edgenum].v=u ;edge[edgenum].flow=0; 30 edge[edgenum].next=head[v]; 31 head[v]=edgenum++; 32 } 33 34 int bfs() 35 { 36 memset(d,0,sizeof(d)); 37 d[from]=1; 38 queue<int> Q; 39 Q.push(from); 40 while (!Q.empty()) 41 { 42 int u=Q.front() ;Q.pop() ; 43 for (int i=head[u] ;i!=-1 ;i=edge[i].next) 44 { 45 int v=edge[i].v; 46 if (!d[v] && edge[i].flow>0) 47 { 48 d[v]=d[u]+1; 49 Q.push(v); 50 if (v==to) return 1; 51 } 52 } 53 } 54 return 0; 55 } 56 57 int dfs(int u,int flow) 58 { 59 if (u==to || flow==0) return flow; 60 int cap=flow; 61 for (int i=head[u] ;i!=-1 ;i=edge[i].next) 62 { 63 int v=edge[i].v; 64 if (d[v]==d[u]+1 && edge[i].flow>0) 65 { 66 int x=dfs(v,min(cap,edge[i].flow)); 67 edge[i].flow -= x; 68 edge[i^1].flow += x; 69 cap -= x; 70 if (cap==0) return flow; 71 } 72 } 73 return flow-cap; 74 } 75 76 int dinic() 77 { 78 int sum=0; 79 while (bfs()) sum += dfs(from,inf); 80 return sum; 81 } 82 83 int main() 84 { 85 while (scanf("%d%d",&n,&m)!=EOF) 86 { 87 memset(head,-1,sizeof(head)); 88 edgenum=0; 89 int s[222],q[222],e[222],t[222]; 90 int time[maxn],cnt=1; 91 memset(time,0,sizeof(time)); 92 int sum=0; 93 for (int i=1 ;i<=n ;i++) 94 { 95 scanf("%d%d%d%d",&s[i],&q[i],&e[i],&t[i]); 96 sum += q[i]*t[i]; 97 time[cnt++]=s[i]; 98 time[cnt++]=e[i]; 99 } 100 sort(time+1,time+cnt); 101 int c=0; 102 for (int i=1 ;i<cnt ;i++) 103 { 104 if (time[c] != time[i]) 105 time[++c]=time[i]; 106 } 107 from=n+c+1; 108 to=from+1; 109 for (int i=1 ;i<=n ;i++) 110 add(from,i,q[i]*t[i]); 111 for (int i=1 ;i<=c ;i++) 112 { 113 add(n+i,to,m*(time[i]-time[i-1])); 114 for (int j=1 ;j<=n ;j++) 115 { 116 if (s[j]<=time[i-1] && time[i]<=e[j]) 117 add(j,n+i,inf); 118 } 119 } 120 if (sum==dinic()) printf("Yes\n"); 121 else printf("No\n"); 122 } 123 return 0; 124 }
标签:
原文地址:http://www.cnblogs.com/huangxf/p/4328884.html