POJ2135_Farm Tour(网络流/费用流)

解题报告

题目传送门

题意:

一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路。

思路:

如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了。

一次mcmf模版。

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
int head[2010],dis[2010],vis[2010],pre[2010],f[2010],cnt,flow,cost,s,t,n,m;
struct node {
    int v,cap,cost,next;
} edge[50000];
void add(int u,int v,int cost,int cap) {
    edge[cnt].v=v,edge[cnt].cost=cost,edge[cnt].cap=cap;
    edge[cnt].next=head[u],head[u]=cnt++;

    edge[cnt].v=u,edge[cnt].cost=-cost,edge[cnt].cap=0;
    edge[cnt].next=head[v],head[v]=cnt++;
}
int _spfa() {
    queue<int>Q;
    Q.push(s);
    for(int i=1; i<=n; i++)dis[i]=inf,pre[i]=vis[i]=f[i]=0;
    dis[s]=0,vis[s]=1,pre[s]=-1,f[s]=inf;
    while(!Q.empty()) {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(edge[i].cap&&dis[v]>dis[u]+edge[i].cost) {
                pre[v]=i;
                f[v]=min(edge[i].cap,f[u]);
                dis[v]=dis[u]+edge[i].cost;
                if(!vis[v]) {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(dis[t]==inf)return 0;
    flow+=f[t];
    cost+=f[t]*dis[t];
    if(flow==2)return 0;
    for(int i=pre[t]; i!=-1; i=pre[edge[i^1].v]) {
        edge[i].cap-=f[t];
        edge[i^1].cap+=f[t];
    }
    return 1;
}
void mcmf() {
    cost=flow=0;
    while(_spfa());
    printf("%d\n",cost);
}
int main() {
    int u,v,w;
    memset(head,-1,sizeof(head));
    scanf("%d%d",&n,&m);
    s=1;
    t=n;
    while(m--) {
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w,1);
        add(v,u,w,1);
    }
    mcmf();
    return 0;
}

Farm Tour

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11402   Accepted: 4230

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

POJ2135_Farm Tour(网络流/费用流)

时间: 2024-10-12 06:40:26

POJ2135_Farm Tour(网络流/费用流)的相关文章

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ训练计划2516_Minimum Cost(网络流/费用流)

解题报告 题意: 有n个商店,m个提供商,k种商品</span> n*k的矩阵,表示每个商店需要每个商品的数目: m*k矩阵,表示每个提供商拥有每个商品的个数 然后对于每个物品k,都有n*m的矩阵 i行j列表示 从j提供商向i商店运送一个k商品的代价是多少 判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用 思路: 建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用, 不过这样

Cyclic Tour HDUOJ 费用流

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 712 Problem Description There are N cities in our country, and M one-way roads connecting them. Now Li

POJ 2195 Going Home(网络流-费用流)

Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17777   Accepted: 9059 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertical

POJ训练计划2195_Going Home(网络流/费用流)

解题报告 题目传送门 思路: bfs建图跑一下费用流就行. #include <iostream> #include <cstdio> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; struct E { int v,cost,cap,next; } edge[100000]; int head[1000],cnt,dis[1000],pre[10

UVa10806_Dijkstra, Dijkstra.(网络流/费用流)(小白书图论专题)

解题报告 思路: 从s->t 再从t->s等同与s->t两次,要求每条路只能走一次,要求最小花费,让每一条边容量为1,跑跑费用流 只要跑出流量为2就结束. #include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 0x3f3f3f3f #define N 5000 #define M 50000 using namespace

[网络流]Farm Tour(费用流

Farm Tour 题目描述 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

luogu P3381 【模板】最小费用最大流 |网络流费用流

#include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=1e4+10,M=2e5+10,inf=0x3f3f3f3f; int n,m,s,t; int nxt[M],head[N],go[M],edge[M],co

POJ 2135 Farm Tour(网络流之费用流)

题目地址:POJ 2135 来回走一遍可以看成从源点到汇点走两遍.将每个点的流量设为1,就可以保证每条边不重复.然后跑一次费用流就行了.当流量到了2之后停止,输出此时的费用. #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <qu