Machines and tasks. Each task has time and cannot be interrupted.
Question: m machines, N candy to be processed, give the working time (S, T) of each candy, as well as the preparation time and cost between sweets, and obtain the minimum cost.
This question began to be affected by the problem that could have been interrupted by time. It started to use time to create a graph, which was a huge headache. Later I learned it and realized that the time was only for cost calculation, but it didn't help with the balance, S --> machine-> candy-> T;
Because each candy needs to be processed once, and the splitting of the candy must go through (-INF ). If one does not exist, there is no solution.
You can also add a group of new points on the Internet. I use the release pressure + infinite flow method.
Kneeling: note! Use INF = 0x3f3f3f, and add N * inf to the end to kill the int! Pay attention to this kind of information later! If you change the value of E [] [3], you also need Longlong!
In this case, I got! However, the name of a variable is mixed up with Wa for one day !!! SB! Do not make variables similar !!!!!!!!!!
#include<iostream>#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;const long long inf=0x3f3f3f3f;const int maxn=505,maxe=80000;int n,m,k;int ss,tt;int head[maxn];long long e[maxe][4];int nume=0;void inline adde(int i, int j, int c,long long w){ e[nume][0]=j;e[nume][1]=head[i];head[i]=nume; e[nume][2]=c;e[nume++][3]=w; e[nume][0]=i;e[nume][1]=head[j];head[j]=nume; e[nume][2]=0;e[nume++][3]=-w;}int inq[maxn];long long d[maxn];int pre[maxn];int prv[maxn];bool spfa(long long &sum){ for(int i=0;i<=tt;i++) { inq[i]=0;d[i]=inf; } queue<int>q; q.push(ss); inq[ss]=1;d[ss]=0; while(!q.empty()) { int cur=q.front(); q.pop(); inq[cur]=0; for(int j=head[cur];j!=-1;j=e[j][1]) { int v=e[j][0]; if(d[v]>d[cur]+e[j][3]&&e[j][2]>0) { d[v]=d[cur]+e[j][3]; pre[v]=j; prv[v]=cur; if(!inq[v]) { q.push(v); inq[v]=1; } } } } if(d[tt]==inf)return 0; int minf=inf; int cur=tt; while(cur!=ss) { minf=minf<e[pre[cur]][2]?minf:e[pre[cur]][2]; cur=prv[cur]; } cur=tt; while(cur!=ss) { e[pre[cur]][2]-=minf; e[pre[cur]^1][2]+=minf; cur=prv[cur]; } sum+=d[tt]*minf; return 1;}long long mincost(){ long long sum=0; while(spfa(sum)); return sum;}int mac[105][2];int cmtime[105][105];int cmony[105][105];int cgtime[105][105];int cgmony[105][105];void build(){ for(int i=1;i<=m;i++) { adde(ss,i,1,0); adde(i,tt,1,0); for(int j=1;j<=n;j++) { if(cmtime[j][i]<mac[j][1]) { if(cmtime[j][i]<=mac[j][0]) adde(i,j+m,1,cmony[j][i]); else adde(i,j+m,1,cmony[j][i]+k*(cmtime[j][i]-mac[j][0])); } } } for(int i=1;i<=n;i++) { adde(i+m,i+m+n,1,-inf); adde(i+m+n,tt,1,0); for(int j=1;j<=n;j++) { if(i!=j&&mac[j][1]>mac[i][1]+cgtime[i][j]) { if(mac[i][1]+cgtime[i][j]<=mac[j][0]) adde(i+m+n,j+m,1,cgmony[i][j]); else adde(i+n+m,j+m,1,cgmony[i][j]+k*(mac[i][1]+cgtime[i][j]-mac[j][0])); } } }}void init(){ nume=0; memset(head,-1,sizeof(head)); ss=0,tt=m+n+n+1;}int main(){ while(~scanf("%d%d%d",&n,&m,&k)&&(n||m||k)) { init(); for(int i=1;i<=n;i++) scanf("%d%d",&mac[i][0],&mac[i][1]); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&cmtime[i][j]); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&cmony[i][j]); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&cgtime[i][j]); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&cgmony[i][j]); } build(); long long ans=mincost()+n*inf; if(ans>=inf) printf("-1\n"); else printf("%I64d\n",ans); }}
Hdu4780 Charge flow (machine tasks are not interrupted)