【Atcoder abc087 D】People on a Line,BFS,图遍历

举报
小哈里 发表于 2022/05/10 23:20:01 2022/05/10
【摘要】 problem D - People on a Line / Time Limit: 2 sec / Memory Limit: 256 MB Score : 400 points Problem St...

problem

D - People on a Line / Time Limit: 2 sec / Memory Limit: 256 MB Score : 400 points Problem Statement There are N people standing on the x -axis. Let the coordinate of Person i be x i . For every i , x i is an integer between 0 and 10 9 (inclusive). It is possible that more than one person is standing at the same coordinate. You will given M pieces of information regarding the positions of these people. The i -th piece of information has the form ( L i , R i , D i ) . This means that Person R i is to the right of Person L i by D i units of distance, that is, x R i − x L i = D i holds. It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values ( x 1 , x 2 , . . . , x N ) that is consistent with the given pieces of information. Constraints 1 ≤ N ≤ 100 000 0 ≤ M ≤ 200 000 1 ≤ L i , R i ≤ N ( 1 ≤ i ≤ M ) 0 ≤ D i ≤ 10 000 ( 1 ≤ i ≤ M ) L i ≠ R i ( 1 ≤ i ≤ M ) If i ≠ j , then ( L i , R i ) ≠ ( L j , R j ) and ( L i , R i ) ≠ ( R j , L j ) . D i are integers. Input Input is given from Standard Input in the following format: N M L 1 R 1 D 1 L 2 R 2 D 2 : L M R M D M Output If there exists a set of values ( x 1 , x 2 , . . . , x N ) that is consistent with all given pieces of information, print Yes; if it does not exist, print No. Sample Input 1 Copy 3 3 1 2 1 2 3 1 1 3 2 Sample Output 1 Copy Yes Some possible sets of values ( x 1 , x 2 , x 3 ) are ( 0 , 1 , 2 ) and ( 101 , 102 , 103 ) . Sample Input 2 Copy 3 3 1 2 1 2 3 1 1 3 5 Sample Output 2 Copy No If the first two pieces of information are correct, x 3 − x 1 = 2 holds, which is contradictory to the last piece of information. Sample Input 3 Copy 4 3 2 1 1 2 3 5 3 4 2 Sample Output 3 Copy Yes Sample Input 4 Copy 10 3 8 7 100 7 9 100 9 8 100 Sample Output 4 Copy No Sample Input 5 Copy 100 0 Sample Output 5 Copy Yes

问题陈述x轴上有N个人。 设人i的坐标为x i。 对于每个i,x i是0到10 9(含)之间的整数。 可能有多个人站在同一座标上。 您将获得M条有关这些人的职位的信息。 第i条信息的格式为(L i,R i,D i)。 这意味着,人R i在人Li的右边,距离为D i距离单位,即x R i-x L i = D i成立。 事实证明,这M条信息中的一些可能不正确。 确定是否存在与给定信息相一致的一组值(x 1,x 2,…,x N)。

solution

/*
+ n个人,每个人的位置为xi。m条信息,ri在li右边d的位置。
+ 求判断是否存在一组信息子集不冲突。
+ 以{l,r,d}为边建立有向图,跑一遍最短路,如果遇到冲突就No
*/
#include<bits/stdc++.h>
using namespace std;
struct node{int u, dist;};
int main(){
	int n, m;  cin>>n>>m;
	vector<vector<node>>G(n);
	for(int i = 0; i < m; i++){
		int l, r, d;  cin>>l>>r>>d;
		l--; r--;
		G[l].push_back(node({r, d}));
		G[r].push_back(node({l, -d}));
	}
	vector<int>dist(n,-2e9);
	for(int i = 0; i < n; i++){
		if(dist[i]!=-2e9)continue;
		dist[i] = 0;
		queue<int>q;
		q.push(i);
		while(q.size()){
			int t = q.front();  q.pop();
			for(auto to:G[t]){
				if(dist[to.u] == -2e9){
					dist[to.u] = dist[t] + to.dist;
					q.push(to.u);
				}else{
					if(dist[to.u] == dist[t] + to.dist)continue;
					else {
						cout<<"No\n"; return 0;
					}
				}
			}
		}
	}
	cout<<"Yes\n";
	return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/113564712

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。