Farm Tour

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:22944   Accepted: 8792

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

Source

USACO 2003 February Green

题意:

 n 个点,m 条无向边,边权为时间。问从 1 到 n 再回到 1 的最短时间。每条边最多走一次。

思路:

(都是双向边)超级源点和结点 1 连边,流量为 2(因为要走两次)费用为0;结点 n 和超级汇点连边,流量为 2 ,费用为0;结点之间相互连边,流量为1(只能走一次)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e3 + 10;
const int M = 5e4 + 10;
struct Edge {
    int to, next, cap, flow, cost;
}edge[M];
int head[N], tot, pre[N], dis[N], n, m, s, t, minCost, maxFlow;
bool vis[N];

void init() {
    tot = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int cap, int cost) { //cap流量 cost花费
    edge[tot].to = v;
    edge[tot].cap = cap;
    edge[tot].cost = cost;
    edge[tot].flow = 0;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].to = u;
    edge[tot].cap = 0;
    edge[tot].cost = -cost;
    edge[tot].flow = 0;
    edge[tot].next = head[v];
    head[v] = tot++;
}

bool spfa(int s, int t) {
    queue<int>q;
    for(int i = 0; i < N; ++i) {
        dis[i] = inf;
        vis[i] = 0;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = 1;
    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) {
            int v = edge[i].to;
            if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if(!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t] == -1) return 0;
    else return 1;
}

int MCMF(int s, int t) {
    minCost = 0;
    maxFlow = 0;
    while(spfa(s, t)) {
        int minn = inf;
        for(int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
            if(minn > edge[i].cap - edge[i].flow)
                minn = edge[i].cap - edge[i].flow;
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
            edge[i].flow += minn;
            edge[i ^ 1].flow -= minn;
            minCost += edge[i].cost * minn;
        }
        maxFlow += minn;
    }
    return maxFlow;
}

int main() {
    int u, v, w;
	while(~scanf("%d%d", &n, &m) && n && m) {
        init();
        s = 0;
        t = n + 1;
        while(m--) {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, 1, w);
            addedge(v, u, 1, w);
        }
        addedge(s, 1, 2, 0);
        addedge(n, t, 2, 0);
        MCMF(s, t);
        printf("%d\n", minCost);
	}
	return 0;
}