Poj2135-farm Tour

Source: Internet
Author: User

Want to see more problem-solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprint Please specify source: http://blog.csdn.net/wangjian8006

FJ bring friends to visit their farms, from their own house to the farm, and then return from the farm to his house, asked to go back to the same road. The number of points in the House is 1, the farm is N, there are many points between 1 and N, given n vertices, m bars, and then m rows have three numbers, the path length of a,b,c for A to C is C, and a to B is a non-edge, now requires from 1 point to N Point return from N point to 1 points of the shortest

Problem-solving ideas: Actually very good map, build a source point to 1 plus a forward edge, the path length is 0, the capacity is 2, the other edges are built without a path length of C, the capacity is 1, and then from N point to the meeting point plus a forward edge, the path is 0, the capacity is 2, and then from the source to the meeting point to find the minimum cost maximum flow The minimum cost is, of course, represented in the diagram as the smallest path

/* Memory 968K time 0MS */#include <iostream> #include <queue> using namespace std; 
#define INF int_max #define MIN (A, b) (a>b?b:a) #define MAXV 1100 #define MAXM 40100 typedef struct{INT s,t,next,w,r;

}edge;
Edge EDGE[MAXM];
int source,sink,n,m,mincost,edgesum;

int HEAD[MAXV],D[MAXV],PARENT[MAXV];
	void Addedge (int a,int b,int c,int r) {edge[edgesum].s=a;
	Edge[edgesum].t=b;
	Edge[edgesum].r=r;
	Edge[edgesum].w=c;
	Edge[edgesum].next=head[a];

	head[a]=edgesum++;
	Edge[edgesum].s=b;
	Edge[edgesum].t=a;
	edge[edgesum].r=0;
	Edge[edgesum].w=-c;
	EDGE[EDGESUM].NEXT=HEAD[B];
head[b]=edgesum++;
	} int SPFA () {Queue <int>q;
	int v,i,tmp;
	
	BOOL VIS[MAXV];
	for (i=0;i<=sink;i++) D[i]=inf;
	memset (vis,false,sizeof (VIS));

	Memset (parent,-1,sizeof (parent));
	Q.push (source);
	Vis[source]=true;
	d[source]=0;
		while (!q.empty ()) {V=q.front (); Q.pop ();

		Vis[v]=false;
			for (I=head[v];i!=-1;i=edge[i].next) {tmp=edge[i].t; if (EDGE[I].R && edge[i].w+d[V]<d[tmp]) {D[TMP]=EDGE[I].W+D[V];
				Parent[tmp]=i;
					if (!vis[tmp]) {Q.push (TMP);
				Vis[tmp]=true;
}}}} return 0;
	} void Mcmf () {int u;
	mincost=0;
		while (1) {SPFA ();

		if (parent[sink]==-1) break;		U=parent[sink];
			There is no need to find the minimum traffic on the Grace Path while (u!=-1) {//Because the minimum flow must be 1 edge[u].r--;
            edge[u^1].r++;
        U=PARENT[EDGE[U].S];
	} Mincost+=d[sink];
	}} int main () {int i,a,b,c;
		while (~SCANF ("%d%d", &n,&m)) {memset (head,-1,sizeof (head));
		edgesum=0,source=0,sink=n+1;
			for (i=1;i<=m;i++) {scanf ("%d%d%d", &a,&b,&c);
			Addedge (a,b,c,1);
		Addedge (b,a,c,1);
		} addedge (source,1,0,2);

		Addedge (n,sink,0,2);
		MCMF ();
	printf ("%d\n", mincost);
} 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.