[NOIP simulation test]: trench Game (Cost Flow)

Title Description

$ NYG $ in the Internet, is to attract advertising into a place called "knife $ 999 $ level" of the game.
The game total of $ n $ th plot points and $ m $ checkpoints, a checkpoint leading to another plot point from a plot point.
One plot point which is called Xinshoucun. No checkpoint leading to the new village, the story does not form a cycle. (Actually a $ 1 to $ root node of the directed acyclic graph).
Of which $ s $ a plot point is called "end point", on behalf of the game we can end this story from the point, because $ NYG $ very noble, so he felt to be played at least $ k $ times a game can be considered clearance (that is, from Xinshoucun come to the end point of $ k $ times). $ NYG $ as a current charge, of course, refreshing soft sister currency clearance this game (or how to meet his noble identity?) Each level monsters has a value that represents the $ NYG $ through this checkpoint by the number of soft sister coins need, of course, the monster will refresh with the death of more and more strong, the first $ x $ times to defeat monsters need $ A_ix + B_i $ soft sister currency, but also self-respecting monster, a monster of $ C_i $ when times were $ money $ NYG killed he would hide, resulting in $ NYG $ can not pass this barrier.
$ NYG $ wondered how many soft sister currency clearance he needs at least to unify from the bank to get (of course, not to save money ah), he handpicked be resolved by you.


Input Format

The first row includes four number $ n, m, k, s $ expressed $ $ n-th plot points, checkpoints $ m $, $ k $ times to play the game, $ S $ a next line end points comprising $ the number s $, $ s $ a representative of end point numbers.
Next $ m $ rows of five positive integer $ x_i, y_i, A_i, B_i , C_i $, $ I $ represents the number of levels from x_i $ $ $ No. plot point is connected to the plot point number y_i $, $ A_i , B_i, C_i $ significance as the title describes.


Output Format

If the clearance is not output $ $ -1, otherwise the output an integer representing the required minimum denomination soft sister.


Sample

Sample input:

6 8 2 2
4 5
1 2 4 0 2
1 3 5 0 2
3 4 1 5 1
2 5 1 0 1
4 6 4 2 2
5 6 0 4 2
1 5 5 9 2
2 6 4 5 2

Sample output:

16


Data range and tips

Sample explained:

From the first $ 1-> 2-> $ 5, $ cost of $ 5.
Second from $ 1-> 3-> $ 4, $ cost of $ 11.

data range:

For $ 30 \% $ data, $ A_i = 0 $.
For additional data $ 20 \% $, the game is a chain.
For $ 100 \% $ data, $ 1 \ leqslant n \ leqslant 1,000,1 \ leqslant m \ leqslant 20,000,1 \ leqslant k \ leqslant 200, A_i, B_i \ geqslant 0,1 \ leqslant C_i \ leqslant k $.
To ensure that the answers in the $ int $ range.


answer

Notes that only $ k $ 200, but who would have thought there is a cost flow it?

In fact, my thinking and problem solutions slight deviation.

Built on top of the side when the record $ A, B $, every once let go spending increase, when the back flow of more off just fine, a drawback is that you can only stream, or you could miss the best answer.

Time complexity: $ \ Theta (m \ times k + n \ times k ^ 2) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
struct rec
{
	int nxt;
	int to;
	int w;
	int f;
	int a;
}e[100001];
int head[5001],cnt=1;
int n,m,k,s;
int S,T;
int que[100001],pre[50001],dis[50001];
bool vis[50001];
int ans;
void add(int x,int y,int w,int f,int a)
{
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	e[cnt].w=w;
	e[cnt].f=f;
	e[cnt].a=a;
	head[x]=cnt;
}
bool bfs()
{
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[S]=0;
	vis[S]=1;
	int he=1,ta=1;
	que[ta]=S;
	while(he<=ta)
	{
		for(int i=head[que[he]];i;i=e[i].nxt)
			if(e[i].w&&dis[que[he]]+e[i].f<dis[e[i].to])
			{
				dis[e[i].to]=dis[que[he]]+e[i].f;
				pre[e[i].to]=i;
				if(!vis[e[i].to])
				{
					vis[e[i].to]=1;
					que[++ta]=e[i].to;
				}
			}
		vis[que[he]]=0;
		he++;
	}
	return dis[T]!=1061109567;
}
void update()
{
	int flag=T;
	while(flag!=S)
	{
		int x=pre[flag];
		ans+=e[x].f;
		e[x].w--;
		e[x^1].w++;
		e[x].f+=e[x].a;
		e[x^1].f-=e[x].a;
		flag=e[x^1].to;
	}
}
int main()
{
	scanf("%d%d%d%d",&n,&m,&k,&s);
	S=n+1,T=n+2;
	add(S,1,k,0,0);
	add(1,S,0,0,0);
	for(int i=1;i<=s;i++)
	{
		int x;
		scanf("%d",&x);
		add(x,T,k,0,0);
		add(T,x,0,0,0);
	}
	for(int i=1;i<=m;i++)
	{
		int x,y,a,b,c;
		scanf("%d%d%d%d%d",&x,&y,&a,&b,&c);
		add(x,y,c,b+a,a);
		add(y,x,0,-b,a);
	}
	cnt=0;
	while(bfs())
	{
		update();
		cnt++;
	}
	if(cnt<k)puts("-1");
	else printf("%d",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11447008.html