poj 2135 Farm Tour 解题报告

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,再从n走到1,然后来回的路不重复的情况下最短路

思路:

看成选择两条从1到n的路然后设计一个超级源点,让它流向起点的流量为2,然后跑最小费用网络流。


这里重点解释一个问题:


为什么:

edge[cnt].from=from;
    edge[cnt].to=to;
    edge[cnt].flow=flow;
    edge[cnt].v=v;
    edge[cnt].next=head[from];
    head[from]=cnt++;

    edge[cnt].from=to;
    edge[cnt].to=from;
    edge[cnt].flow=0;
    edge[cnt].v=-v;
    edge[cnt].next=head[to];
    head[to]=cnt++;

要构一个反向的花费为-v流量为0的边,而

        add(s,e,v,1);
        add(e,s,v,1);


这里却构两次;



我们这么想吧:假设这并不是双向边,而是单向的从s到e,

那么这里只应该写成:add(s,e,v,1);

普通网络流建图时是不是要在跑完某条路时建一条它的反向边呢?而这里我们还需要考虑一下作为链式前向星怎么建它的反向边?很麻烦,于是我们可以先把它的反向边建出来,但是流为零,这样在跑过某条路时只需要更改它的反向边的流就行了



具体代码如下:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#define inf 1000000007
using namespace std;
struct node{
    int from,to,v,flow,next;
};
node edge[50005];
int cnt,head[50005],E,father[50005],length[50005];//father用来储存第i个点的来源的那条边,注意是边,length用来储存到第i点的最近距离
void add(int from,int to,int v,int flow){//链式前向星存边
    edge[cnt].from=from;
    edge[cnt].to=to;
    edge[cnt].flow=flow;
    edge[cnt].v=v;
    edge[cnt].next=head[from];
    head[from]=cnt++;

    edge[cnt].from=to;
    edge[cnt].to=from;
    edge[cnt].flow=0;
    edge[cnt].v=-v;
    edge[cnt].next=head[to];
    head[to]=cnt++;
}

bool spfa(){
    memset(father,-1,sizeof(father));
    for(int i=0;i<=E;i++)length[i]=inf;
    int vis[50005];
    memset(vis,0,sizeof(vis));
    vis[0]=1;
    queue<int >q;
    length[0]=0;
    q.push(0);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head[now];i!=-1;i=edge[i].next){
            if(edge[i].flow && length[edge[i].to]>length[edge[i].from]+edge[i].v){
                length[edge[i].to]=length[edge[i].from]+edge[i].v;
                father[edge[i].to]=i;//如果这个最短路被更新,那么说明通过这条边到edge[i].to更近
                if(!vis[edge[i].to]){//注意注意,这里存的是edge[i].to而不是i我因此找了二十分钟bug才发现
                                     //(并且我也不是第一次在这里出错了......)
                    vis[edge[i].to]=1;
                    q.push(edge[i].to);
                }
            }
        }
    }
    if(father[E]==-1)return false;
    return true;
}
int solve(){
    int ans=0;
    while(spfa()){
        int now=father[E];
        while(now!=-1){
            edge[now].flow-=1;
            edge[now^1].flow+=1;//这个^运算就比较巧妙了,任何一个边的反向边都是它^1,
            now=father[edge[now].from];
        }
        ans+=length[E];
    }
    return ans;
}
int main(){
    memset(head,-1,sizeof(head));
    int n,m;
    cnt=0;
    scanf("%d%d",&n,&m);
    E=n+1;
    for(int i=1;i<=m;i++){
        int s,e,v;
        scanf("%d%d%d",&s,&e,&v);
        add(s,e,v,1);
        add(e,s,v,1);
    }
    add(0,1,0,2);
    add(n,E,0,2);//其实不用设计超级汇点拉,不过没关系,这不是重点
    cout<<solve()<<endl;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值