POJ2135 Farm Tour

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17203 Accepted: 6640

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

—————————————————————————————————

题目大意:就是从1走到n然后再走回来,一条边只能走一次,要求路径最短。

解题思路:其实很好建图,建一个源点到1加一条有向边,路径长度为0,容量为2,其他边建无向边路径长度为c,容量为1,再从n点到汇点加一条有向边,路径为0,容量为2,然后从源点到汇点求最小费用最大流,当然最小费用在图中表示就是最小路径


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 100100
#define MAXM 1000100

int vis[MAXN],d[MAXN],pre[MAXN],aa[MAXN];

struct Edge
{
    int u, v, c, cost, next;
} edge[MAXM];
int s[MAXN], cnt;

void init()
{
    cnt = 0;
    memset(s, -1, sizeof(s));
}

void add(int u, int v, int c, int cost)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cost = cost;
    edge[cnt].c = c;
    edge[cnt].next = s[u];
    s[u] = cnt++;
    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cost = -cost;
    edge[cnt].c = 0;
    edge[cnt].next = s[v];
    s[v] = cnt++;
}

bool spfa(int ss, int ee,int &flow,int &cost)
{
    queue<int> q;
    memset(d, INF, sizeof d);
    memset(vis, 0, sizeof vis);
    d[ss] = 0, vis[ss] = 1, pre[ss] = 0, aa[ss] = INF;
    q.push(ss);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = s[u]; ~i; i = edge[i].next)
        {
            int v = edge[i].v;
            if (edge[i].c>0&& d[v]>d[u] + edge[i].cost)
            {
                d[v] = d[u] + edge[i].cost;
                pre[v] = i;
               aa[v] = min(aa[u], edge[i].c);
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    if (d[ee] == INF) return 0;
    flow += aa[ee];
    cost += d[ee]*aa[ee];
    int u = ee;
    while (u != ss)
    {
        edge[pre[u]].c -= aa[ee];
        edge[pre[u] ^ 1].c += aa[ee];
        u = edge[pre[u]].u;
    }
    return 1;
}

int MCMF(int ss, int ee)
{
    int cost = 0, flow=0;
    while (spfa(ss, ee, flow, cost));

    return cost;
}



int main()
{
  int n,m,u,v,c;
  while(~scanf("%d%d",&n,&m))
  {
      init();
      for(int i=1;i<=m;i++)
      {
          scanf("%d%d%d",&u,&v,&c);
          add(u,v,1,c);
           add(v,u,1,c);
      }
     add(0,1,2,0);
     add(n,n+1,2,0);
      printf("%d\n",MCMF(0,n+1));


  }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值