POJ 2135:Farm Tour 邻接表最小费用最大流

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13824 Accepted: 5253

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,有很多条路,挑两条完全不同的路,使得这两条路的和权值最小。

一开始想spfa跑两次,跑完第一次就将图中的边删除。但发现这种思路是错的,最终求的是和最小,这种贪心的思想可能会由于删去的边的原因使得下一条路径权值很大,所以不对。

然后就还是网络流,建立一个源点0和汇点N+1,0到1的边代价为0,容量为2(因为要跑两次)。N到N+1的边代价为0,容量为2。这两跑出来的结果就是网络流的最小代价,也就是走两次的最小和。

上一次写的时候最小费用最大流的时候用的是邻接矩阵,感觉很方便,这次没办法时间原因,只好邻接表,写的时候各种不熟练。

图论到现在真的感觉自己还没有入门,好多有趣的题目还没有见到,慢慢加油。

代码:

#pragma warning(disable:4996)  
#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;

#define INF 0x7fffffff
#define MAXN 1005

ll res;
int n;
int N, M;
struct EDGE
{
	int v;
	int reverse;
	int cap;
	int cost;
	int next;
}edge[MAXN*MAXN];

int stac[MAXN],head[MAXN], d[MAXN], vis[MAXN], pre[MAXN];

void addedge(int u, int v, int cost,int cap)
{
	edge[n].v = v;
	edge[n].cost = cost;
	edge[n].cap = cap;
	edge[n].next = head[u];
	edge[n].reverse = n + 1;
	head[u] = n++;

	edge[n].v = u;
	edge[n].cost = -cost;
	edge[n].cap = 0;
	edge[n].next = head[v];
	edge[n].reverse = n - 1;
	head[v] = n++;
}

void input()
{
	int i;
	int s, e, ww;

	scanf("%d%d", &N, &M);
	
	memset(edge, 0, sizeof(edge));
	memset(head, -1, sizeof(head));
	
	n = 0;
	for (i = 1; i <= M; i++)
	{
		scanf("%d%d%d", &s, &e, &ww);
		addedge(s, e, ww, 1);
		addedge(e, s, ww, 1);
	}
	addedge(0, 1, 0, 2);
	addedge(N, N + 1, 0, 2);
}

bool spfa()
{
	int i, top;
	for (i = 0; i <= N + 1; i++)
	{
		d[i] = INF;
		vis[i] = 0;
		pre[i] = i;
	}
	top = 0;
	d[0] = 0;
	stac[++top] = 0;
	vis[0] = 1;

	while (top)
	{
		int u = stac[top--];
		for (i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].v;
			if (edge[i].cap&&d[v] > d[u] + edge[i].cost)
			{
				d[v] = d[u] + edge[i].cost;
				pre[v] = i;//与之前邻接矩阵记录点不同的是,这里记录的是边

				if (!vis[v])
				{
					vis[v] = 1;
					stac[++top] = v;
				}
			}
		}
		vis[u] = 0;
	}
	if (d[N + 1] == INF)
	{
		return false;
	}
	else
	{
		return true;
	}
}

void compute()
{
	int sum = INF;
	int u, p;
	for (u = N + 1; u != 0; u = edge[edge[p].reverse].v)//然后这里通过reverse,找到逆边的终点即连接着的起点
	{
		p = pre[u];//p代表连接的边
		sum = min(sum, edge[p].cap);
	}
	for (u = N + 1; u != 0; u = edge[edge[p].reverse].v)
	{
		p = pre[u];
		edge[p].cap -= sum;
		edge[edge[p].reverse].cap += sum;
		res += sum*edge[p].cost;
	}
}

void solve()
{
	res = 0;
	while (spfa())
		compute();
	printf("%lld\n", res);
}

int main()
{
	//freopen("i.txt", "r", stdin);
	//freopen("o.txt", "w", stdout);

	input();
	solve();

	//system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值