POJ3169——Layout && hdu3592——World Exhibition

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

这两题完全一个意思,都是差分约束的水题

找到约束条件然后建边求最短路即可

#include <map>  
#include <set>  
#include <list>  
#include <stack>  
#include <vector>  
#include <queue>  
#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  

using namespace std;

const int N = 1010;
const int M = 20010;
const int inf = 0x3f3f3f3f;

queue <int>qu; 
int tot, n, m;
int head[N];
int num[N];
int dist[N];
int in_queue[N];

struct node
{
	int weight;
	int next;
	int to;
}edge[M];

void addedge(int from, int to, int weight)
{
	edge[tot].weight = weight;
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

bool spfa(int v0)
{
	memset (dist, inf, sizeof(dist) );
	memset (in_queue, 0, sizeof(in_queue) );
	dist[v0] = 0;
	in_queue[v0]++;
	while ( !qu.empty() )
	{
		qu.pop();
	}
	qu.push(v0);
	while ( !qu.empty() )
	{
		int u = qu.front();
		qu.pop();
		for (int i = head[u]; ~i; i = edge[i].next)
		{
			int v = edge[i].to;
			if (dist[v] > dist[u] + edge[i].weight)
			{
				dist[v] = dist[u] + edge[i].weight;
				in_queue[v]++;
				if (in_queue[v] == n + 1)
				{
					return false;
				}
				qu.push(v);
			}
		}
	}
	return true;
}

int main()
{
	int  m1, m2;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &n, &m1, &m2);
		memset (head, -1, sizeof(head) );
		tot = 0;
		int u, v, w;
		while (m1--)
		{
			scanf("%d%d%d", &u, &v, &w);
			addedge(u, v, w);
		}
		while (m2--)
		{
			scanf("%d%d%d", &u, &v, &w);
			addedge(v, u, -w);
		}
		for (int i = 1; i <= n; ++i)
		{
			addedge(i + 1, i, 0);
		}
		bool flag = spfa(1);
		if (!flag)
		{
			printf("-1\n");
			continue;
		}
		for (int i = 1; i <= n; ++i)
		{
			if (dist[i] != inf)
			{
				continue;
			}
			flag = false;
			break;
		}
		if (!flag)
		{
			printf("-2\n");
			continue;
		}
		printf("%d\n", dist[n]);
	}
	return 0;
}


[kuangbin带你飞]专题九 连通图 题解报告 专题链接 关于tarjan A - Network of Schools  原题地址 本题有2个问题,第一个是要算最少要给多少个点软件,才能使所有点都可以收到副本 第二个是要算最少加多少条边,使得图变成强连通 1:tarjan求强连通,然后缩点,计算入度为0的强连通分量 2:设现在有a个入度为0的点,b个出度为0的点(缩完点后的点),最合理的加边方法肯定是从出度为0的点 阅读详情

相关推荐

POJ 3169 LayoutHDU 3592差分约束

http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线。有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离。如果没有输出-1,如果可以随便排输出-2,否则输出最大的距离。 思路: 对于第一种 B - A ...

weixin_30692143的博客 227

POJ-3169-LayoutHDU-3592-World Exhibition(线性约束,板子)

题目链接:http://poj.org/problem?id=3169 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意:n头牛,编号1~n,给出ml个关系(牛与牛之间最大的距离)md个关系(牛与牛之间最小的距离),编号小的总在编号大的前面(可以站在一起),问,牛1到牛n的最远距离是多少。无解输出-1,无穷解输出-2,正解输出di...

永远鲜红の幼月的博客 262

poj3169 layout差分约束的证明。。。

在Acm论文中看到的,一起学习一下,至少解决了我不少疑问。 3.3 例题3——Layout 题意简述: 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。FJ有N(2 一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L。另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D。给出ML条关于两头奶牛间有好感的描述,再给出MD条关于两头奶牛间存有反感

zhang20072844的专栏 3632

POJ3169 Layout & 【HDU3592 World Exhibition 差分约束

传送门:

poursoul 634

HDU3592 World Exhibition & POJ3169 Layout差分约束

题意:N个人排成一条线,按顺序编号1到N,有可能多个人站在一个点上。有x个约束(a,b,c),表示a和b互有好感,他俩的距离不能超过c,还有y个约束(a,b,c),表示a和b互相讨厌,他俩的距离不能少于c。求1到N的最大距离,无解输出-1,有无穷远输出-2.   思路:差分约束裸题,建图跑最短路就行,以1为起点,N为终点。如果dis[N]为inf,说明两个数字互不影响,就返回-2。 两题的内...

hcx11333的博客 225

Codeforces Round #277.5 (Div. 2)B——BerSU Ball

B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The Berland State University is hosting a ballroom dance

Alex 1590

LightOJ1003---Drunk(拓扑排序判环)

One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let m

Alex 1492

LightOJ1009---Back to Underworld (bfs染色)

The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none knows who will win. The humans want to know who will survive finally. But humans are afraid of going to

Alex 1424

Codeforces Round #288 (Div. 2)---D. Tanya and Password

D. Tanya and Password time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While dad was at work, a little girl Tanya deci

Alex 1423

Timus Online Judge1627--- Join

1627. Join Time limit: 4.0 second Memory limit: 64 MB Businessman Petya recently bought a new house. This house has one floor with n × m square rooms, placed in rectangular lattice. Some roo

Alex 1411

hdu1179——Ollivanders: Makers of Fine Wands since 382 BC.

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 964    Accepted Submission(s): 539 Prob

Alex 1406

SPOJ104. Highways

104. Highways Problem code: HIGH In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't

Alex 1382

LightOJ1002---Country Roads (最短路变形)

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of m

Alex 1244

Codeforces Round #294 (Div. 2)---E. A and B and Lecture Rooms

A and B are preparing themselves for programming contests.The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1 corridors s

Alex 1242

hdu1535——Invitation Cards

Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2173    Accepted Submission(s): 1056 Problem Description In the age o

Alex 1182

ACdream1236——Burning Bridges

Burning Bridges Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description       Ferry Kingdom is a nice litt

Alex 1149

LightOJ1012---Guilty Prince (并查集)

Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a ne

Alex 1141

hdu1281---棋盘游戏

Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。 所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可

Alex 1130

hdu4081---Qin Shi Huang's National Road System

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4043    Accepted Submission(s): 1393 Problem Descri

Alex 1093
上一篇: hdu2647——Reward
下一篇: POJ2983——Is the Information Reliable?
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值