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

     
  • 相关阅读:
    tomcat禁止查看文件目录
    jsp中的时间操作
    用 iframe 解决下拉框与层之冲突
    不要把灯泡放进口中
    一个高效简洁的Struts分页方法
    非常Cool的 网页特效(背景藏,alert样式)
    让table中的英文和数字换行
    隐藏多行文本框的滚动条
    tomcat默认首页设置
    如何利用回车来进行焦点转移呢
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273620.html
Copyright © 2011-2022 走看看