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;
    }

     
  • 相关阅读:
    C# 数据处理——(包括但不限)浮点数设置小数点后位数 (转)
    c#下怎么判断一个字符串是否可以转换为double类型(转)
    C++ string字符串按分隔符分割成一个数组(转)
    C#浮点数保留位(转)
    linq与lambda写法对照(转)
    C# DataTable 和List之间相互转换的方法(转)
    c#写入Mysql中文显示乱码 解决方法(转)
    微博营销之企业微博运营方案实用篇
    “最美店主”走红网络,或成国内欧美第一店
    月饼西施PK愤怒的小鸟,这个中秋狂掀DIY风
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273620.html
Copyright © 2011-2022 走看看