Bzoj 1877: [SDOI2009] morning run (minimum cost maximum flow) [Provincial selection Plan series]

Source: Internet
Author: User
Tags bool time limit
1877: [SDOI2009] Morning runTime Limit:4 Sec Memory limit:64 MB
submit:1999 solved:1079
[Submit] [Status] [Discuss] DescriptionElaxia recently had a crush on karate, and he set up a fitness program for himself, such as push-ups, sit-ups and so on, but so far, he has only survived the morning run. Now give a map near the school, which contains n intersections and M streets, Elaxia can only run from one intersection to another, and the streets intersect only at intersections. Elaxia every day from the dormitory run to school, to ensure that the dormitory number 1, the school number is n. Elaxia's morning running plan is carried out by cycle (including several days), because he does not like to follow the repeated route, so in a cycle, the daily morning running route will not intersect (at the crossroads), bedroom and school is not a crossroads. Elaxia stamina is not very good, he hopes to run in a cycle as short as possible, but also want the training cycle to contain as long as possible. In addition to practicing karate, Elaxia other time spent studying and looking for mm above, all he would like to ask you to help him design a suit to meet his requirements of the morning run plan. InputFirst line: two number n,m. Indicates the number of intersections and streets. The next M-line, 3 numbers per line, indicates that there is a street (unidirectional) with a length of c between junction A and intersection B (a,b,c). OutputTwo number, the first number is the longest period of the number of days, the second number to meet the maximum number of days of the shortest distance length. Sample Input7 10
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
2 5 5
3 6 6
5 7 1
6 7 1 Sample Output2 11 HINT

For 30% of data, n≤20,m≤120.
For 100% of data, n≤200,m≤20000. Source

Day1


Days as long as possible-----> maximum Flow
Shortest distance-----> Minimum cost

Minimum cost maximum flow template problem
Code:

#include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include < cmath> #include <cstring> #include <string> #include <climits> #include <queue> #include < stack> #include <map> #include <set> #define N 20020 #define M 2000002 #define INF 1<<26 using NAMESPAC
e std;
	inline int read () {int X=0,f=1;char ch; while (ch< ' 0 ' | |
	Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
	while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;

} int N,m,s,t,ans;
int head[n],pos=-1,pp[n]; struct Edge{int cost,flow,u,v,next;}
E[M]; void Add (int a,int b,int cost,int flow) {pos++;e[pos].v=b,e[pos].u=a,e[pos].cost=cost,e[pos].flow=flow,e[pos].next=
Head[a],head[a]=pos;}

void Insert (int a,int b,int cost,int flow) {Add (A,b,cost,flow); add (b,a,-cost,0);}
Queue<int>q;int Dis[n];bool Vis[n];
	BOOL Spfa () {for (int i=s;i<=n*2;i++) Pp[i]=-1,vis[s]=0,dis[i]=inf; Vis[s]=1;
	Q.push (s);d is[s]=0; while (! Q.emPty ()) {int U=q.front ();
		Q.pop (); vis[u]=0;
			for (int i=head[u];i!=-1;i=e[i].next) {int v=e[i].v;
			if (e[i].flow<=0) continue;
				if (dis[v]>dis[u]+e[i].cost) {dis[v]=dis[u]+e[i].cost;pp[v]=i; if (!vis[v]) {vis[v]=1;
			Q.push (v);}
}}}return Dis[t]!=inf;
	} int MCMF () {int ret=0;
		while (SPFA ()) {int minf=inf+1;
		for (int i=pp[t];i!=-1;i=pp[e[i].u]) minf=min (Minf,e[i].flow);
		for (int i=pp[t];i!=-1;i=pp[e[i].u]) E[i].flow-=minf,e[i^1].flow+=minf;
	Ret+=dis[t]*minf;ans+=minf;
}return ret;
	} void Init () {memset (head,-1,sizeof (Head));} int main () {N=read (), M=read (); Init ();
		for (int i=1;i<=m;i++) {int x=read (), Y=read (), Z=read ();
	Insert (x+n,y,z,1);
	}s=1,t=n;
	for (int i=2;i<n;i++) insert (i,i+n,0,1);
	Insert (s,s+n,0,inf); int RET=MCMF ();
printf ("%d%d\n", Ans,ret);
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.