Hdu4780 Charge flow (machine tasks are not interrupted)
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 about the cost, 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
#include
#include
#include
#includeusing 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
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
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); }}