POJ 2135 Farm Tour minimum fee inflow door template

Source: Internet
Author: User
Tags prev

Test instructions

Ask for point 1 to N and then from Point N to point 1 without the shortest path of the same route.

Analysis:

It is easy to build a set of data for the shortest two times:

4 5
1 2 1
1 3 100
2 4 100
2 3 1
3 4 1

The next minimum fee flow template is good.

Code:

POJ 2135//sep9 #include <iostream> #include <queue> using namespace std;
const int maxn=2048;
const int maxm=20024;	
struct Edge {int v,f,w,nxt;
}E[4*MAXM+10];
int g[maxn+10];
int nume,src,sink;
Queue<int> Q;
BOOL INQ[MAXN+10];
int dist[maxn+10];

int prev[maxn+10],pree[maxn+10];

int n,m;	
	void Addedge (int u,int v,int c,int w) {e[++nume].v=v;e[nume].f=c;e[nume].w=w;e[nume].nxt=g[u];g[u]=nume;
E[++nume].v=u;e[nume].f=0;e[nume].w=-w;e[nume].nxt=g[v];g[v]=nume; } bool Find_path () {while (!
	Q.empty ()) Q.pop ();	
	memset (dist,0x7f,sizeof (Dist));
	memset (inq,false,sizeof (INQ));
	Q.push (SRC);
	Inq[src]=true;
	dist[src]=0; while (! Q.empty ()) {int U=q.front ();
		Q.pop ();
		Inq[u]=false; for (int i=g[u];i;i=e[i].nxt) {if (e[i].f>0&&dist[u]+e[i].w<dist[e[i].v]) {dist[e[i].v]=dist[u]+e[i].
				W
				Prev[e[i].v]=u;	
				Pree[e[i].v]=i;
					if (!INQ[E[I].V]) {Q.push (E[I].V);
				Inq[e[i].v]=true; }}}} return dist[sink]<dist[maxn]?trUe:false;
	} int Min_cost_flow (int f) {int res=0;	
		while (f>0) {if (Find_path () ==false) return-1;
		int d=f;
		for (int v=sink;v!=src;v=prev[v]) d=min (D,E[PREE[V]].F);
		F-=d;
		Res+=d*dist[sink];
			for (int v=sink;v!=src;v=prev[v]) {e[pree[v]].f-=d;
		E[pree[v]^1].f+=d;			
}} return res;
	} void Init () {memset (g,0,sizeof (g));
nume=1;
	} int main () {scanf ("%d%d", &n,&m);
	Init ();
		while (m--) {int a,b,c;
		scanf ("%d%d%d", &a,&b,&c);
		Addedge (A-1,B-1,1,C);
	Addedge (B-1,A-1,1,C);
	} src=0,sink=n-1;
	printf ("%d\n", Min_cost_flow (2));	
return 0;  }


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.