Layout(Belman Fold or BelmanFold+SPFA 差分约束)

Layout

成绩10开启时间2018年02月20日 星期二 16:05
折扣0.8折扣时间2018年12月30日 星期日 19:05
允许迟交关闭时间2018年12月30日 星期日 16:05

Description

Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). 

Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated. 

Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.

Input

Line 1: Three space-separated integers: N, ML, and MD. 

Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. 

Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

Output

Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

Sample Input

4 2 1
1 3 10
2 4 20
2 3 3

Sample Output

27

Hint

Explanation of the sample: 

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. 

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

Source

USACO 2005 December Gold

解法一:没用SPFA优化,裸的Bellman_Ford



#include <iostream>
using namespace std;
#define INF 0x7ffffff
#define MaxP 1005
#define MaxE 10005
int N, ML, MD, id;


int dis[MaxP];
struct Edge
{
	int u;
	int v;
	int w;
}edges[MaxE];

void AddEdge(int u,int v,int w)
{
	edges[id].u = u;
	edges[id].v = v;
	edges[id].w = w;
	id++;
}


void Bellman_Ford(int s, int goal)
{
	dis[1] = 0;
	for (int i = 2; i <= N; i++)
		dis[i] = INF;

	for (int i = 1; i <= N - 1;i++)
	for (int j = 0; j < ML + MD; j++)
	{
		if (dis[edges[j].v]>dis[edges[j].u] + edges[j].w)
			dis[edges[j].v] = dis[edges[j].u] + edges[j].w;

	}

	//判断是否出现负环,如果出现负环则无解
	for (int j = 0; j < ML + MD; j++)
	{
		if (dis[edges[j].v]>dis[edges[j].u] + edges[j].w)
		{
			printf("-1\n");
			return;
		}

	}

	//如果dis[N]==INF,说明1到N可以是任意值
	if (dis[N] == INF)
	{
		printf("-2\n");
		return;
	}

	//如果有解,输出dis[N]
	cout << dis[N]<<endl;

}

int main()
{
	//freopen("1.txt", "r", stdin);
	cin >> N >> ML >> MD;
	int u, v, w;
	for (int i = 0; i < ML; i++)
	{
		cin >> u >> v >> w;
		AddEdge(u, v, w);
	}
	for (int i = 0; i < MD; i++)
	{
		cin >> u >> v >> w;
		AddEdge(v,u,-w);
	}

	Bellman_Ford(1, N);
}


解法二:

参考文章:https://blog.csdn.net/lianai911/article/details/45497795

//解法二:用了SPFA优化的Bellman_Ford
#include <iostream>
#include<queue>
using namespace std;
#define INF 0x7ffffff
#define MaxP 1005
#define MaxE 10005
int N, ML, MD, id;

struct ndoe
{
	int to;
	int w;
	int next;
}edges[MaxE];

int d[MaxP]; int outque[MaxP]; int head[MaxP];
bool vis[MaxP];

void AddEdge(int u,int v,int w)
{
	edges[id].to = v;
	edges[id].w = w;
	edges[id].next = head[u];
	head[u] = id++;//用来记录当前u点连接的是第几条边,这个写法比较巧妙
}

void SPFA(int s,int N)
{
	//memset(head, -1, sizeof(head));
	int flag = 0;
	for (int i = 1; i <= N; i++)
		d[i] = INF;
	d[s] = 0;
	queue<int> q;
	q.push(s);
	vis[s] = 1;
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		vis[u] = 0;
		outque[u]++;
		if (outque[u] > N + 1)//如果一个点出队大于N次,则出现负环
		{
			flag = 1;   
			break;
		}
		for (int i = head[u]; i != -1; i = edges[i].next)
		{
			int tmp = d[u] + edges[i].w;
			if (d[edges[i].to] > tmp)
			{
				d[edges[i].to] = tmp;
				if (!vis[edges[i].to])
				{
					vis[edges[i].to] = 1;
					q.push(edges[i].to);
				}
			}
		}
	}

	if (flag == 1)
	{
		cout << -1 << endl;
		return;
	}
	if (d[N] == INF)
		cout << -2 << endl;
	else
		cout << d[N]<<endl;
}

int main()
{
	//freopen("1.txt", "r", stdin);
	int u, v, w;
	cin >> N >> ML >> MD;
	memset(head, -1, sizeof(head));
	for (int i = 0; i < ML; i++)
	{
		cin >> u >> v >> w;
		AddEdge(u, v, w);
	}
	for (int i = 0; i < MD; i++)
	{
		cin >> u >> v >> w;
		AddEdge(v, u, -w);
	}
	SPFA(1, N);
	return 0;
}

虽然说用了SPFA优化,但我感觉没有多大提升啊

最后送上一篇好文

SPFA算法详解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值