poj 2135 Farm Tour(最小费用最大流)

来源:互联网 发布:淘宝新势力周时间表 编辑:程序博客网 时间:2024/05/17 22:39
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14241 Accepted: 5430

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 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6

最小费用最大流问题。

构建超级源点s和汇点t,s连1,流量为2,n连t,流量为2;

#include<iostream>using namespace std;#include<queue>#define maxn 0x7fffffff#include<algorithm>#include<string.h>#define MS(a,b) memset(a,b,sizeof(a))int n,m,vis[10005],dist[10005],head[10005],f,pre[10005],maxflow;struct node{  int s,e,next,cost,cap;}edge[100005];void add(int s,int e,int cap,int cost){    edge[f].s=s;edge[f].e=e;edge[f].cap=cap;    edge[f].cost=cost;edge[f].next=head[s];    head[s]=f++;    edge[f].s=e;edge[f].e=s;edge[f].cap=0;edge[f].cost=-cost;    edge[f].next=head[e];    head[e]=f++;}int spfa(int s,int t) {   int u,k,i;   queue<int>q;   MS(pre,-1);   MS(vis,0);  for(i=0;i<=n+2;i++)    dist[i]=maxn;   q.push(s);   dist[s]=0;   vis[s]=1;    while(!q.empty())    {      u=q.front();      q.pop();      vis[u]=0;      for(i=head[u];i!=-1;i=edge[i].next)      {          int v=edge[i].e;          if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost)          {           dist[v]=dist[u]+edge[i].cost;           pre[v]=i;           if(!vis[v])           {            q.push(v);             vis[v]=1;           }          }      }    }    if(dist[t]==maxn)return 0;    return 1; } int MCMF(int s,int t) {   int minflow,mincost=0,flow,i;   while(spfa(s,t))   {        minflow=maxn;       for(i=t;i!=s;i=edge[pre[i]].s)        minflow=min(minflow,edge[pre[i]].cap);//找增广路上的最小流量边       flow+=minflow;       for(i=t;i!=s;i=edge[pre[i]].s)       {           edge[pre[i]].cap-=minflow;           edge[pre[i]^1].cap+=minflow;       }       mincost+=minflow*dist[t];   }   maxflow=flow;   return mincost; } int main() {     int i,a,b,w,s,t;     cin>>n>>m;     s=0;t=n+1;     MS(head,-1);     f=0;     for(i=0;i<m;i++)     {       cin>>a>>b>>w;       add(a,b,1,w);       add(b,a,1,w);     }     add(s,1,2,0);     add(n,t,2,0);     cout<<MCMF(s,t)<<endl;     return 0; }


0 0
原创粉丝点击