[网络流]Farm Tour(费用流

时间:2023-03-09 10:01:43
[网络流]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 <= 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.

输入

* 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.

输出

A single line containing the length of the shortest tour.

样例输入

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

样例输出

6

代码:
 #include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int total;
const int MAXN = ;
const int INF = ;
struct Edge
{
int u, v, cap, cost;
int next;
} edge[];
int edgenum;
int head[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN];
void init()
{
edgenum = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[edgenum].u = u;
edge[edgenum].v = v;
edge[edgenum].cap = cap;
edge[edgenum].cost = cost;
edge[edgenum].next = head[u];
head[u] = edgenum++;
edge[edgenum].u = v;
edge[edgenum].v = u;
edge[edgenum].cap = ;
edge[edgenum].cost = -cost;
edge[edgenum].next = head[v];
head[v] = edgenum++;
}
bool spfa(int s, int t, int n)//找到一条增广路
{
int i, u, v;
queue <int> qu;
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
for(i = ; i <= n; i++) dist[i] = INF;
vis[s] = true;
dist[s] = ;
qu.push(s);
while(!qu.empty())
{
u = qu.front();
qu.pop();
vis[u] = false;
for(i = head[u]; i != -; i = edge[i].next)
{
v = edge[i].v;
if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
{
dist[v] = dist[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
qu.push(v);
vis[v] = true;
}
}
}
}
if(dist[t] == INF) return false;
return true;
}
int min_cost_max_flow(int s, int t, int n)
{
int flow = ; // 总流量
int i, minflow, mincost;
mincost = ;
while(spfa(s, t, n))
{
minflow = INF + ;
for(i = pre[t]; i != -; i = pre[edge[i].u])
if(edge[i].cap < minflow)
minflow = edge[i].cap;
flow += minflow;
for(i = pre[t]; i != -; i = pre[edge[i].u])
{
edge[i].cap -= minflow;
edge[i^].cap += minflow;
}
mincost += dist[t] * minflow;
}
total = flow; // 最大流
return mincost;
}
int main()
{
int n, m;
int u, v, c;
while (scanf("%d%d", &n, &m) != -)
{
init();
int s = ;
int t = n + ;
while (m--)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u, v , , c);
addedge(v, u , , c);
}
addedge(s, , , );
addedge(n, t, , );
int ans = min_cost_max_flow(s, t, n + );
printf("%d\n", ans);
}
return ;
}