bzoj1061 [Noi2008] Volunteer recruitment
Original title address : http://www.lydsy.com/JudgeOnline/problem.php?id=1061
Test Instructions:
A project takes n days to complete, with the first day I need an AI person at least. A total of M-type volunteers can be recruited. In which class I can work from Si days to ti days, the recruitment fee is per person ci yuan. Bubu hopes to recruit enough volunteers with as little as possible to find the best cost of recruiting programs.
Data Range
1≤n≤1000,1≤m≤10000, the other data involved in the topic are not more than 2^31-1.
The following:
The byvoid of God
is to construct a formula that says that each variable appears one negative at a time.
The auxiliary variable is then added for the processing of the >=.
For positive and negative variables as inflow and outflow, satisfy inequalities ..... =0 is the flow balance.
Code:
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <
Queue> using namespace std;
const int n=505;
const int m=805;
const int inf=0x3f3f3f3f;
Queue<int> Q;
int n,m,head[n],to[4*m],nxt[4*m],w[4*m],id,u[m],v[m],w[m],s,t,num=1,dep[n];
BOOL Vis[n];
void build (int u,int v,int ww) {num++; To[num]=v;
Nxt[num]=head[u]; Head[u]=num;
W[NUM]=WW;
num++; To[num]=u;
NXT[NUM]=HEAD[V]; Head[v]=num;
w[num]=0;
} bool BFs () {memset (vis,0,sizeof (VIS)); Q.push (S); Vis[s]=1;
Dep[s]=1; while (! Q.empty ()) {int U=q.front ();
Q.pop ();
for (int i=head[u];i;i=nxt[i]) {int v=to[i]; if (w[i]<=0| |
VIS[V]) continue; Vis[v]=1;
dep[v]=dep[u]+1;
Q.push (v);
}} return vis[t]; } int dfs (int u,int d) {if (!d| | U==T) return D;
int ret=0;
for (int i=head[u];i;i=nxt[i]) {int v=to[i]; if (w[i]<=0| | Dep[v]!=dEP[U]+1) continue;
int Flow=dfs (V,min (D,w[i])); Ret+=flow;
D-=flow; W[i]-=flow;
W[i^1]+=flow;
if (!d) break;
} if (!ret) dep[u]=-1;
return ret;
} int main () {scanf ("%d%d%d", &n,&m,&id);
for (int i=1;i<=m;i++) scanf ("%d%d%d", &u[i],&v[i],&w[i]);
for (int i=1;i<=m;i++) {if (i==id) s=u[i],t=v[i];
else if (W[i]<=w[id]) build (u[i],v[i],w[id]-w[i]+1), build (v[i],u[i],w[id]-w[i]+1);
} int ret=0;
while (BFS ())) Ret+=dfs (S,inf);
printf ("%d\n", ret);
return 0;
}