2018 ACM-ICPC Syrian Collegiate Programming Contest

Problem H: Bugged System

思路来源**(https://blog.csdn.net/Mitsuha_/article/details/84455292)**
Mr. Light is visiting a city with a “smart” metro system. Or so it seems …

There are exactly n stations in a line, where the ith station is located at a distance xi from the beginning of the line. You can check into some station, travel between the stations as many times as you want in both directions, and check out from another station. The metro card will track the sum of distances you traveled and charge you accordingly once you check out of your destination station.

However, there seems to be a bug in the system; if you happen to check in and out from the same station, you will be charged 0 credit. This creates the possibility of a scenario where one person traveling from stations a to b, and another person traveling from stations b to a, they can now meet up at some station and swap their cards. Therefore when they arrive, they both will pay 0 credit. Check the explanation of the first sample test for another scenario.

A person can go to any number of stations and wait as long as they like. Two people that meet at the same station can swap their cards.

You are given the starting and destination stations for m people traveling along the metro. Is it possible for all m people to check out from their destination stations and pay 0 credit? If so, print the minimum total distance they must travel to achieve this.

Input
The first line of input contains a single integer T (1 ≤ T ≤ 3700), the number of test cases.

The first line of each test case contains two space-separated integers n and m (2 ≤ n, m ≤ 2 × 105), the number of stations and the number of people that will be using the metro stations.

The second line contains n space-separated integers x1, x2, …, xn (0 ≤ xi ≤ 2 × 106), where xi is the distance of the ith station from the beginning of the line.

Each of the following m lines contains two integers si and di (1 ≤ si, di ≤ n, si ≠ di), representing the starting and destination stations of ith person.

The sum of n over all test cases doesn’t exceed 2 × 106, the same is true for m.

Output
For each test case, output on a single line with the minimum total distance all m people need to travel to pay 0 credit.

If it is not possible, output  - 1 on a line.

Example
Input
2
3 3
10 50 25
1 2
2 3
3 1
4 2
1 10 5 3
1 2
4 3
Output
80
-1

Note
First sample explanation:

There are three metro cards, checked in at stations s1, s2, and s3 and with person p1, p2, and p3, respectively.

Person p1 goes to station s2, and swaps cards with person p2. Now person p1 can check out from station s2 with 0 credit (distance traveled 40).

Person p2 now has the card that was checked in from station s1 and goes to station s3 with it. He swaps that card with person p3 and checks out from station s3 with the new card he has (distance traveled 25).

Person p3 now goes to station s1 with the card that was also checked in from station s1 and checks out there with 0 credit (distance traveled 15).

Sum of traveled distances is 40 + 25 + 15 = 80.

  • 没啥好说的,队里打训练赛,在这题耗了太长时间,由于对并查集没有深入理解,一开始竟然想用并查集来解决后来发现无法克服环等一系列原因,因此此题没有AC
  • 要保证每个人都不用付钱,那么最后一定形成一个环,最短路是所有人路径之和否则输出-1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
int main()
{
	//freopen("bugged.in","r",stdin);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		scanf("%d %d",&n,&m);
		ll a[200005],x[200005];//数组a用来判断是否构成一个环,如果构成环那么操作后数组里应全为0的位置
		memset(a,0,sizeof(a));   
		for(int i=1;i<=n;i++){
			scanf("%lld",&x[i]);
		}
		ll sum=0;    //求路径之和
		for(int i=1;i<=m;i++){
			int l,r;
			scanf("%d %d",&l,&r);
			a[l]++;
			a[r]--;
			sum+=abs(x[l]-x[r]); 
		}
		int ff=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=0)
				ff=1;
		}
		if(ff==1) cout<<"-1"<<endl;
		else printf("%lld\n",sum);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值