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;
}


内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值