首页 > 其他分享 >E. Rendez-vous de Marian et Robin

E. Rendez-vous de Marian et Robin

时间:2024-10-14 20:01:11浏览次数:8  
标签:node vous Marian int de done push id dis

链接:https://codeforces.com/problemset/problem/2014/E
题面:

思路:
最短路首选dijkstra,这题也是这样,但是难点在于如何记录有马的时间这个状态。采取方法就是没有马的情况下正反跑一遍dijkstra,然后记录有马的位置的时间,从每个有马的地方重新dijkstra。拿四个数组,取ans = min ( ans , max (min(horse1,nohorse1),min(horsen,nohorsen) ) )
代码:

#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
#include<map>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

#define int long long 
const int INF = LLONG_MAX;
const int N = 2e5 + 3;
struct edge
{
	int from, to, w;
	bool horse;
	edge(){}
	edge(int a, int b, int v) { from = a, to = b; w = v; horse = false; }
	edge(int a, int b, int v,bool ax) { from = a, to = b; w = v; horse = ax; }

};
vector<edge>e[N];
vector<int>id;
struct node
{
	int id; int n_dis;
	node(int b, int c) { id = b, n_dis = c; }
	node(){}
	bool operator<(const node& a)const
	{
		return n_dis > a.n_dis;
	}
};
int n, m , h;
int disa[N], disf[N];
int disah[N], disfh[N];
bool done[N];
bool havehorse[N];
void dijkstraa()
{
	//先跑两个基础的dijk
	int  s = 1;
	for (int i = 1; i <= n; i++) { disa[i]=disah[i] = INF; done[i] = 0; }
	disa[s]= 0;
	if (havehorse[s])disah[s] = 0;
	priority_queue<node>Q;
	Q.push(node(s, disa[s]));
	while (!Q.empty())
	{
		node u = Q.top();
		Q.pop();
		if (done[u.id])continue;
		done[u.id] = true;
		for (int i = 0; i < e[u.id].size(); i++)
		{
			edge y = e[u.id][i];
			if (done[y.to])continue;
			if (disa[y.to] > y.w + u.n_dis)
			{
				disa[y.to] = y.w + u.n_dis;
				if (havehorse[y.to])disah[y.to] = disa[y.to];
				
				Q.push(node(y.to, disa[y.to]));
			}
		}
	}
	s = n;
	for (int i = 1; i <= n; i++) { disf[i]=disfh[i] = INF; done[i] = 0; }
	disf[s] = 0;
	if (havehorse[s])disfh[s] = 0;
	Q.push(node(s, disf[s]));
	while (!Q.empty())
	{
		node u = Q.top();
		Q.pop();
		if (done[u.id])continue;
		done[u.id] = 1;
		for (int i = 0; i < e[u.id].size(); i++)
		{
			edge y = e[u.id][i];
			if (done[y.to])continue;
			if (disf[y.to] > y.w + u.n_dis)
			{
				disf[y.to] = y.w + u.n_dis;
				if (havehorse[y.to])disfh[y.to] = disf[y.to];
				Q.push(node(y.to, disf[y.to]));
			}
		}
	}
	for (int v : id)
	{
		Q.push(node(v, disah[v]));
	}
	memset(done, 0, sizeof(done));
	while (!Q.empty())
	{
		node u = Q.top();
		Q.pop();
		if (done[u.id])continue;
		done[u.id] = true;
		for (int i = 0; i < e[u.id].size(); i++)
		{
			edge y = e[u.id][i];
			if (done[y.to])continue;
			if (disah[y.to] > y.w / 2 + u.n_dis)
			{
				disah[y.to] = y.w / 2 + u.n_dis;
				Q.push(node(y.to, disah[y.to]));
			}
		}
	}
	for (int v : id)
	{
		Q.push(node(v, disfh[v]));
	}
	memset(done, 0, sizeof(done));
	while (!Q.empty())
	{
		node u = Q.top();
		Q.pop();
		if (done[u.id])continue;
		done[u.id] = true;
		for (int i = 0; i < e[u.id].size(); i++)
		{
			edge y = e[u.id][i];
			if (done[y.to])continue;
			if (disfh[y.to] > y.w / 2 + u.n_dis)
			{
				disfh[y.to] = y.w / 2 + u.n_dis;
				Q.push(node(y.to, disfh[y.to]));
			}
		}

	}

}

void init()
{
	memset(havehorse, 0, sizeof(havehorse));
	memset(done, 0, sizeof(done));
	id.clear();
	for (int i = 0; i <= n; i++)e[i].clear();
}
signed main()
{
	IOS;
	int t; cin >> t;
	while (t--)
	{
		cin >> n >> m >> h;
		init();
		for (int i = 0; i < h; i++) { int x; cin >> x; id.push_back(x); havehorse[x] = 1; }
		for (int i = 0; i < m; i++)
		{
			int u, v, w;
			cin >> u >> v >> w;
			e[u].push_back(edge(u, v, w));
			e[v].push_back(edge(v, u, w));
		}
		dijkstraa();
		int ans = LLONG_MAX;
		for (int i = 1; i <= n; i++)
		{
			if (disa[i] != INF and disf[i] != INF )
			{
				int mini = min(disfh[i], disf[i]);
				int minisc = min(disa[i], disah[i]);
				ans = min(ans, max(mini,minisc));
			}
		}
		if (ans == INF)cout << -1 << '\n';
		else cout << ans << '\n';


	}
	return 0;
}

这也太长了些

标签:node,vous,Marian,int,de,done,push,id,dis
From: https://www.cnblogs.com/zzzsacmblog/p/18464900

相关文章

  • Failed to clean project: Failed to delete
    原文链接:Failedtocleanproject:Failedtodelete–每天进步一点点(longkui.site)使用IDEA编辑代码,电脑突然死机。然后重启电脑后,执行mavenclean指令。结果报错:Failedtoexecutegoalorg.apache.maven.plugins:maven-clean-plugin:2.5:clean(default-clean)onproj......
  • 【Unity塔防游戏素材包】Tower Defense Pack - Low Poly 3D Art
    TowerDefensePack-LowPoly3DArt是一个专为塔防类游戏设计的Unity插件,提供丰富的低多边形3D资源,涵盖了塔防游戏所需的各种元素,如塔楼、敌人、环境道具等。这些资源风格统一,兼具简约和精致,非常适合开发具有卡通风格、低多边形风格的塔防游戏。主要功能:多样化的塔......
  • 题解:P1709 [USACO5.5] 隐藏口令 Hidden Password
    ProblemLink[USACO5.5]隐藏口令HiddenPassword题目描述求最小表示法的开头字母在原字符串的位置。Solution最小表示法板子,双指针解决即可。Code#include<iostream>#include<algorithm>#include<string.h>#include<cstring>#include<cmath>#include<cstdio>......
  • [PA2021] Od deski do deski 题解
    好题好题,难者不会会者不难,我是前者。实际上加入就可以合法的数是很好计算的。考虑现在所有前缀合法串后的字符实际上都可以满足条件。容易想到根据是否合法设置状态。设\(f_{i,j}/g_{i,j}\)表示现在填第\(i\)个数,有\(j\)个填了就合法的数,现在的串合法/不合法。那么有转......
  • (nice!!!)(LeetCode) 1884. 鸡蛋掉落-两枚鸡蛋(动态规划 dfs递归和递推 || 数学)
    题目:1884.鸡蛋掉落-两枚鸡蛋方法一:动态规划dp+递归dfs+记忆化搜索。时间复杂度0(n^2)。C++版本:classSolution{public: //状态sta[i]表示:i层找到f所需要的最小操作次数intsta[1010];inttwoEggDrop(intn){ //层数为0时,直接返回0if(n==0......
  • Cascader 级联选择器代码实现
    当一个数据集合有清晰的层级结构时,可通过级联选择器Cascader 逐级查看并选择,在开发过程中,非常常见,解决选择列表数量过多的情况。结果1、组件<el-cascader:props="props":options="unitData"v-model="query.UnitName"size="small"style="margin-left:10px;margin-rig......
  • C++在vscode中的code runner配置/环境配置
    C++在vscode中快捷运行(coderunner)一、配置tasks.json在vscode中创建文件夹或打开文件夹,会发现文件夹下多了一个.vscode文件夹,在该文件夹下创建tasks.json文件,并添加一下内容{"version":"2.0.0","tasks":[{"type":"shell","la......
  • 题解:AT_abc370_c [ABC370C] Word Ladder
    题目传送门luogu观看简要题意给两个序列\(S\)和\(T\),输出的第一个数是它能改变的总个数,后面跟着的第\(i\)个是改变\(i\)个数之后,字典序最小的结果。思路当\(S\)与\(T\)相等的话,那就无法改变了,直接输出\(0\)。对于总数只要\(T_i\neS_i\)那它就可以改,所以只......
  • 闯关leetcode——94. Binary Tree Inorder Traversal
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/binary-tree-inorder-traversal/description/内容Giventherootofabinarytree,returntheinordertraversalofitsnodes’values.Example1:Input:root=[1,null,2,3]Outpu......
  • 闯关leetcode——100. Same Tree
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/same-tree/description/内容Giventherootsoftwobinarytreespandq,writeafunctiontocheckiftheyarethesameornot.Twobinarytreesareconsideredthesameifthey......