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

     
  • 相关阅读:
    【HDOJ6666】Quailty and CCPC(模拟)
    【2019 Multi-University Training Contest 8】
    分布式锁的理解
    反射工具类【ReflectionUtils】
    Maven常用命令
    maven常用命令介绍
    mysql 优化策略(如何利用好索引)
    centos7搭建svn服务器及客户端设置
    Centos7 配置subversion
    Centos7更改网卡名称Eth0
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273620.html
Copyright © 2011-2022 走看看