Road

Road
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 23, Accepted users: 20
Problem 12934 : No special judgement
Problem description

In one country, there are N cities, numbered 1 to N. A civil engineer has to build public roads that connect all the cities together, i.e., it must be possible to travel from all cities to any other cities, maybe going through multiple cities. His team has surveyed several routes (candidate road between any two cities). Each route is a bi- directional connection between two cities. He can build a bi-directional road on the surveyed route for a specific cost (The shorter the route is the cheaper the road).

This engineer has never planned a road system in advance. He would just pick one of the routes based on his preference, and build a road until all the cities are connected.

Right now this engineer is going to build a road from the city p to the city q. With pressure from the government to reduce the cost, he asks you to write a program to decide, if he should build this road or not. Your program should say yes if the building of this road guaranteed that it can be part of the shortest road system that connects all cities together. Otherwise, your program should say no.


Input

First line of input is a number of test cases T ≤ 10.

Each test case start with a line contains 4 integers N, M, p, and q.

N (2 ≤ N ≤ 10 000) is the number of cities in this road system.

M (1 ≤ M ≤ 20 000) is the number of surveyed routes.

p and q (1 ≤ p ≤ N and 1 ≤ q ≤ N) indicate the route between two cities,

that the engineer asks if he can build a road or not. Then, the each of the following M lines contains 3 numbers u, v, and w (1 ≤ u ≤ N, 1 ≤ v ≤ N, 1 ≤ w ≤ 400 000) indicates that there are the bidirectional route of length w between u and v. The length of each road in the current system is unique. And, there is only one possible road between two cities. The input guarantees that at least one road system has a route between any two cities. All numbers are integer.


Output

For each test case, display YES if the engineer can build a road p-q as part of the shortest road system. Otherwise, print NO.


Sample Input
3
2 1 1 2
1 2 4
3 3 2 3
1 2 1
1 3 2
2 3 3
4 5 3 4
1 2 1
1 3 3
3 4 2
1 4 4
4 2 5
Sample Output
YES
NO
YES
#include<iostream>//不想说了,我再也不用模版了,自己做自己想 
#include<cstring>//加入变后合并这两个点 
#include<cstdlib>//8.24.ROAD 
#include<algorithm>
#include<cstdio>
#define MAXM 40010
#define MAXN 20010
using namespace std;
bool flag;
int n,m,strx,stry,strnum,k,ans;
struct EDGE
{
	int u,v,w;
}edge[MAXM];

bool cmp(EDGE a,EDGE b)
{
	return a.w<b.w;
}

void read(int *x)
{
    char ch;
    (*x) = 0;
    while((ch = getchar()) > 47 && ch < 58)
    {
        (*x) *= 10;
        (*x) += ch - '0';
    }
}



void union_point(int a,int b)//??
{
	for(int i=0;i<m;i++)
	{
		if(edge[i].u==b)
		edge[i].u=a;
		if(edge[i].v==b)
		edge[i].v=a;
	}
}
void kruskal()
{
	sort(edge+1,edge+1+m,cmp);
	for(int i=1;i<=m;i++)
	{
		if(edge[i].u!=edge[i].v)
		{
			//cout<<edge[i].u<<" "<<edge[i].v<<endl;
			if(i==strnum)
			{
				flag=1;
				return;
			}
			ans+=edge[i].w;
			k++;
			union_point(edge[i].u,edge[i].v);
		}
		if(k==n-1)
	    return;
	}
}

int main()
{
    int cas;
	cin>>cas;
	while(cas--)
	{
		flag=0;
		k=0;ans=0;
		cin>>n>>m>>strx>>stry; 
		for(int i=1;i<=m;i++)
		{
			int x,y,value;
			cin>>x>>y>>value;
			edge[i].u=x;
			edge[i].v=y;
			edge[i].w=value;
		}
		strnum=-1;
		for(int i=1;i<=m;i++)
		{
			if(edge[i].u==strx&&edge[i].v==stry)
			{
			
			strnum=i;
			break;
		    }
			if(edge[i].u==stry&&edge[i].v==strx)
			{
			
			strnum=i;
		    break;
		    }
		}
		kruskal();
		if(flag)
		printf("YES\n");
		else
		printf("NO\n");
	}
	return 0;	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值