(最小生成树+dfs_高效率)Blinking Stalkers(CD1529)
  W79oLdECuAdO 2023年11月02日 31 0


好不容易,,直接用DFS,,DIJ,,BFS都不行,,还得先处理一下,用VECTOR,然后效率才会提上去,,,呜呜,,学到了,



#include<string.h>
#include <iostream>
#include<stack>
#include<algorithm>
#include<time.h>
#include<queue>
#include<cstdio>
using namespace std;

int n,m;
int map[1001][1001];
int v;
bool ans;
int to;
bool vist[1001];
int set[1001];
vector<int> q[1001];

struct my
{
	int x;
	int y;
	int v;
}
go[1000001];

bool cmp(my a,my b)
{
	return a.v<b.v;
}

int find(int k)
{
	if (set[k]!=k)
		return find(set[k]);
	return k;
}

bool dfs(int from)
{
	//cout<<from<<endl;
	int i,j,k;
	vist[from]=true;
	for (i=0;i<q[from].size();i++)
	{
		if (q[from][i]==to)
		{
			if (map[from][to]<=v)
				return true;
			else
				return false;
		}
		else if (!vist[q[from][i]]&&map[from][q[from][i]]<=v&&dfs(q[from][i]))
			return true;
	}
	return false;
	
}

void dij(int from)
{
	int i,j,k;
	int hi[1001];
	for (i=1;i<=n;i++)
	{
		hi[i]=map[from][i];
	}
	j=from;
	k=n-1;
	while (k--)
	{
		for (i=1;i<=n;i++)
		{
			if (!vist[i]&&hi[i]&&hi[i]<=v)
			{
				j=i;
			}
		}
		if (j==to)
		{
			ans=true;
			return;
		}
		for (i=1;i<=n;i++)
		{
			if (!vist[i]&&map[i][j]&&hi[i]>map[i][j])
				hi[i]=map[i][j];
		}
	}
}

void bfs(int from)
{
	int i,j,k;
	queue<int> q;
	while (!q.empty())
	{
		q.pop();
	}
	q.push(from);
	vist[from]=true;
	while (!q.empty())
	{
		int d=q.front();
		q.pop();
		if (d==to)
		{
			ans=true;
			return;
		}
		for (i=1;i<=n;i++)
		{
			if (!vist[i]&&map[d][i]&&map[d][i]<=v)
			{
				vist[i]=true;
				q.push(i);
			}
		}
	}
}

int main()
{
	freopen("fuck.txt","r",stdin);
	int i,j,k;
	int t;
	scanf("%d",&t);
	int from;
	int num=0;
	while (t--)
	{
		num++;
		scanf("%d%d",&n,&m);
		for (i=1;i<=n;i++)
		{
			for (j=1;j<=n;j++)
			{
				map[i][j]=0;
			}
		}
		for (i=0;i<m;i++)
		{
			scanf("%d%d%d",&go[i].x,&go[i].y,&go[i].v);
		}
		sort(go,go+m,cmp);
		for (i=1;i<=n;i++)
		{
			set[i]=i;
			q[i].clear();
		}
		k=0;
		for (i=0;i<m;i++)
		{
			if (find(go[i].x)!=find(go[i].y))
			{
				set[set[go[i].x]]=set[set[go[i].y]];
				map[go[i].x][go[i].y]=map[go[i].y][go[i].x]=go[i].v;
				q[go[i].x].push_back(go[i].y);
				q[go[i].y].push_back(go[i].x);
			}
		}

		printf("Case #%d:\n",num);
		scanf("%d",&k);
		while (k--)
		{
			scanf("%d%d%d",&from,&to,&v);
			ans=false;
			for (i=1;i<=n;i++)
				vist[i]=false;
			//dfs(from);
			//dij(from);
			//bfs(from);
			if (dfs(from))
				printf("YES\n");
			else
				printf("NO\n");
		}
	}

	return 0;
}



Blinking Stalkers

Time Limit: 2000 ms Memory Limit: 65536 kB Solved:8 Tried: 28


Description



In Starcraft II, Stalker is an unusual kind of unit for its amazing ability called "blink". Blink means you can move from one position to another one immediately as long as the distance between the two position is less than D. Now, there is N stalkers on some islands, and everyone have a target island to go. And of course, targets of them can be variable. Because any stalker can't move on water, they can only blink between islands. For some strange reason, only some particular pairs of island are allowed to blink. Your task is to determine whether they can move to their target island.



Input



In the first line of input there is an integer T, which indicates there are T test cases in the input.
For each test case, the first line contains two integer n(2<=n<=1000), m(1<=m<=100000), meaning there are n islands and m pairs island where stalkers can blink. Then m lines followed, each line contain three integer u, v, w, (1<=u, v<=n), (0< w <= 1000000), which means a stalker can blink from u to v or v to u, and the distance is w. Then a single line contains a integer Q(1<=Q<=3000), which indicates there are Q stalkers. Then Q lines followed, each line contain three integer a, b, c, (1<=a, b<=n), (0 < c <= 1000000), meaning this stalker start at island a and target island is b, the distance it can blink is c. You can assume that the graph described in every test case is connected.



Output



For each test case, output Q+1 lines. First, output "Case #C: ", where C is the number of test case, from 1 to T. Then output Q lines, if the ith stalker can move to its target island using its ability for finite times, output “YES” in a single line, otherwise output “NO”.



Sample Input



1
3 3
1 2 2
1 3 3
2 3 2
2
1 3 1
1 3 2



Sample Output



Case #1:
NO
YES



Source





【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

W79oLdECuAdO
作者其他文章 更多