【CF1343F】 Restore the Permutation by Sorted Segments

题目

题目描述
You are given an undirected unweighted graph consisting of nn vertices and mm edges (which represents the map of Bertown) and the array of prices pp of length mm . It is guaranteed that there is a path between each pair of vertices (districts).

Mike has planned a trip from the vertex (district) aa to the vertex (district) bb and then from the vertex (district) bb to the vertex (district) cc . He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used pp is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road.

You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip.

You have to answer tt independent test cases.

输入格式
The first line of the input contains one integer tt ( 1 \le t \le 10^41≤t≤10
4
) — the number of test cases. Then tt test cases follow.

The first line of the test case contains five integers n, m, a, bn,m,a,b and cc ( 2 \le n \le 2 \cdot 10^52≤n≤2⋅10
5
, n-1 \le m \le min(\frac{n(n-1)}{2}, 2 \cdot 10^5)n−1≤m≤min(
2
n(n−1)

,2⋅10
5
) , 1 \le a, b, c \le n1≤a,b,c≤n ) — the number of vertices, the number of edges and districts in Mike’s trip.

The second line of the test case contains mm integers p_1, p_2, \dots, p_mp
1

,p
2

,…,p
m

( 1 \le p_i \le 10^91≤p
i

≤10
9
), where p_ip
i

is the ii -th price from the array.

The following mm lines of the test case denote edges: edge ii is represented by a pair of integers v_iv
i

, u_iu
i

( 1 \le v_i, u_i \le n1≤v
i

,u
i

≤n , u_i \ne v_iu
i



=v
i

), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ( v_i, u_iv
i

,u
i

) there are no other pairs ( v_i, u_iv
i

,u
i

) or ( u_i, v_iu
i

,v
i

) in the array of edges, and for each pair (v_i, u_i)(v
i

,u
i

) the condition v_i \ne u_iv
i



=u
i

is satisfied. It is guaranteed that the given graph is connected.

It is guaranteed that the sum of nn (as well as the sum of mm ) does not exceed 2 \cdot 10^52⋅10
5
( \sum n \le 2 \cdot 10^5∑n≤2⋅10
5
, \sum m \le 2 \cdot 10^5∑m≤2⋅10
5
).

输出格式
For each test case, print the answer — the minimum possible price of Mike’s trip if you distribute prices between edges optimally.

题意翻译
给出一个有 nn 个点,mm 条边的无向图和一个长为 mm 的权值序列 ww。

你可以随意安排边权(每条边权对应 ww 中的一个数,不可以重复)。

求 aa 到 bb 的最短路与 bb 到 cc 的最短路的和的最小值。

输入输出样例
输入 #1复制
2
4 3 2 3 4
1 2 3
1 2
1 3
1 4
7 9 1 5 7
2 10 4 8 5 6 7 3 3
1 2
1 3
1 4
3 2
3 5
4 2
5 6
1 7
6 7
输出 #1复制
7
12
说明/提示
One of the possible solution to the first test case of the example:

One of the possible solution to the second test case of the example:

思路

有两条性质
如果一个数出现在所有子排列中出现两次及以上,则这个数肯定不是原排列里的最后一个数
如果有一个数出现次数只有一次,则它可能是最后一个也可能是第一个数
然后就没了

代码

const ll MAXN=210;
ll t,n;
ll a[MAXN][MAXN],pos[MAXN],ans[MAXN];
bool ok(){
	for(int now=2;now<=n;now++){
		for(int i=1;i<n;i++){
			ll r=a[i][0],x=-1;
			for(int k=1;k<=a[i][0];k++){
				ll b=a[i][k];
				if(!pos[b])x=b;
				else if(pos[b]>=now-a[i][0]+1)r--;
			}
			if(r==1&&x!=-1){
				ans[now]=x;
				pos[x]=now;
				break;
			}
		}
		if(ans[now]==0)return false;
	}
	return true;
}
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<n;i++){
			cin>>a[i][0];
			for(int j=1;j<=a[i][0];j++)cin>>a[i][j];
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				pos[j]=0;ans[j]=0;
			}
			ans[1]=i;
			pos[i]=1;
			if(ok())break;
		}
		for(int i=1;i<=n;i++)cout<<ans[i]<<' ';
		cout<<endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值