zoukankan      html  css  js  c++  java
  • poj--2135--Farm Tour(最小费用最大流)

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

    Status

    Description

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

    To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

    He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

    Input

    * Line 1: Two space-separated integers: N and M.

    * Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

    Output

    A single line containing the length of the shortest tour.

    Sample Input

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

    Sample Output

    6
    
    
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<algorithm>
    using namespace std;
    #define MAXN 1010
    #define MAXM 50000+100
    #define INF 0x3f3f3f
    struct node
    {
    	int u,v,cap,flow,cost,next;
    }edge[MAXM];
    int dis[MAXN],head[MAXN],pre[MAXN];
    bool vis[MAXN];
    int m,n,top;
    void init()
    {
    	top=0;
    	memset(head,-1,sizeof(head));
    }
    void add(int a,int b,int w,int c)
    {
    	node E1={a,b,w,0,c,head[a]};
    	edge[top]=E1;
    	head[a]=top++;
    	node E2={b,a,0,0,-c,head[b]};
    	edge[top]=E2;
    	head[b]=top++;
    }
    void getmap()
    {
    	int a,b,c;
    	while(m--)
    	{
    		scanf("%d%d%d",&a,&b,&c);
    		add(a,b,1,c);
    		add(b,a,1,c);
    	}
    	add(0,1,2,0);
    	add(n,n+1,2,0);
    }
    bool SPFA(int s,int t)
    {
    	queue<int>q;
    	memset(dis,INF,sizeof(dis));
    	memset(vis,0,sizeof(vis));
    	memset(pre,-1,sizeof(pre));
    	dis[s]=0;
    	vis[s]=true;
    	q.push(s);
    	while(!q.empty())
    	{
    		int u=q.front();
    		q.pop();
    		vis[u]=0;
    		for(int i=head[u];i!=-1;i=edge[i].next)
    		{
    			node E=edge[i];
    			if(dis[E.v]>dis[u]+E.cost&&E.cap>E.flow)
    			{
    				dis[E.v]=dis[u]+E.cost;
    				pre[E.v]=i;
    				if(!vis[E.v])
    				{
    					vis[E.u]=1;
    					q.push(E.v);
    				}
    			}
    		}
    	}
    	return pre[t]!=-1;
    }
    void mcmp(int s,int t,int &cost)
    {
    	cost=0;
    	while(SPFA(s,t))
    	{
    		int MIN=INF;
    		for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
    		{
    			node E=edge[i];
    			MIN=min(MIN,E.cap-E.flow);
    		}
    		for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
    		{
    			edge[i].flow+=MIN;
    			edge[i^1].flow-=MIN;
    			cost+=edge[i].cost*MIN;
    		}
    	}
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		init();
    		getmap();
    		int cost=0;
    		mcmp(0,n+1,cost);
    		printf("%d
    ",cost);
    	}
    	return 0;
    }

     
  • 相关阅读:
    POJ 1584 A Round Peg in a Ground Hole(计算几何凸包)
    POJ 1113 Wall(计算几何凸包的周长)
    HDU 1864 最大报销额(01背包应用)
    NYOJ 303 序号互换(规律)河南第四届ACM省赛
    POJ 2031 Building a Space Station(三维空间中最小生成树Prim算法)
    POJ 1265 Area(计算几何Pick定理)
    POJ 2470 || SDUT 2356 Ambiguous permutations(简单规律)
    SDUT 1918 运送物资(并查集的应用)
    POJ 2471 || SDUT 2357 Bullshit Bingo(字符串处理)
    python爬虫热点项目—滑块验证码项目(以Bilili为例)
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273620.html
Copyright © 2011-2022 走看看