当前位置:网站首页>2019 Shaanxi Provincial race K-variant Dijstra
2019 Shaanxi Provincial race K-variant Dijstra
2022-07-25 15:34:00 【Brother Tazi is here】
Preface :
A class with certain restrictions Dijstra Find the shortest question type
The main idea of the topic :
Single starting point , The shortest path with multiple endpoints .
Every node to , There will be d i d_i di A monster comes out and will d i d_i di link i i i The edge of the point is blocked . Their goal is to maximize your shortest path . Ask you the shortest result
Topic ideas :
practice :
Will end d i = 0 d_i=0 di=0. Then run from the finish line Dijstra, Take it out of the queue every time v v v, if d v > 0 d_v>0 dv>0, Make d v − − d_v-- dv−−, continue .
otherwise : Update this point d i s t dist dist
correctness :
1. Why start from the end , Because it stopped after the end , Don't consider this point d i d_i di 了 .
Even if the end d = 1 e 9 d=1e9 d=1e9 It's useless ?
2. Think that every time you take it out of the queue, it must be the best one in the current situation . d i − − d_i-- di−− It means that we must use i i i A monster of to block this side .
Complexity :
The upper bound of relaxation times is the number of edges . So complexity : O ( m l o g m ) O(mlogm) O(mlogm)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define fi first
#define se second
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int a[maxn];
template <typename T>
void read(T & x){
x = 0;T f = 1;char ch = getchar();while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();}while (isdigit(ch)){
x = x * 10 + (ch ^ 48);ch = getchar();}x *= f;}
template <typename T>
void write(T x){
if(x < 0){
putchar('-');x = -x;}if (x > 9)write(x / 10);putchar(x % 10 + '0');}
struct Node{
int id , v;
bool operator < (const Node & a) const
{
return v > a.v;
}
};
priority_queue<Node> q;
vector<pii> e[maxn];
int dist[maxn] , d[maxn] , bk[maxn];
void init (int n){
for (int i = 1 ; i <= n ; i++){
e[i].clear();
dist[i] = -1;
bk[i] = d[i] = 0;
}
}
int main()
{
int t; read(t);
while (t--){
int n , m , k; read(n);read(m);read(k);
init (n);
vi st;
for (int i = 1 ; i <= k ; i++){
int x; read(x);
st.push_back(x);
}
for (int i = 1 ; i <= n ; i++) read(d[i]);
for (int i = 1 ; i <= m ; i++){
int x , y , z;
read(x);
read(y);
read(z);
e[x].push_back({
y , z});
e[y].push_back({
x , z});
}
for (auto & g : st){
d[g] = 0;
dist[g] = 0;
q.push({
g , 0});
}
while (q.size()){
Node u = q.top();
int id = u.id , dd = u.v;
q.pop();
//cout << " Now in " << id << " also " << d[id] << " Time " << dd << endl;
if (d[id]){
d[id] = max(d[id] - 1 , 0);
continue;
}
if (bk[id]) continue;
bk[id] = 1;
dist[id] = dd;
//cout << " Will succeed dist" << id << " Assigned as " << dist[id] << endl;
for (auto & g : e[id]){
int v = g.first , w = g.second;
if (dist[v] == -1 || dist[v] > dd + w)
q.push({
v , dd + w});
}
}
printf("%d\n" , dist[1]);
}
return 0;
}
边栏推荐
- Once spark reported an error: failed to allocate a page (67108864 bytes), try again
- 2021上海市赛-B-排序后dp
- In depth: micro and macro tasks
- Pat grade a 1152 Google recruitment (20 points)
- ML - natural language processing - Basics
- JVM-垃圾收集器详解
- Binary complement
- Xcode添加mobileprovision证书文件报错:Xcode encountered an error
- MySQL heap table_ MySQL memory table heap Usage Summary - Ninth Five Year Plan small pang
- wait()和sleep()的区别理解
猜你喜欢

Pytorch学习笔记--常用函数总结3

Application of C language array in Sanzi chess -- prototype of Queen n problem

Games101 review: linear algebra

伤透脑筋的CPU 上下文切换

wait()和sleep()的区别理解

MySQL transactions and mvcc

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

小波变换--dwt2 与wavedec2

Spark partition operators partitionby, coalesce, repartition

ML - natural language processing - Basics
随机推荐
ios 面试题
ML - 自然语言处理 - 基础知识
p4552-差分
C # fine sorting knowledge points 10 generic (recommended Collection)
Reflection - Notes
使用cpolar建立一个商业网站(如何购买域名)
本地缓存--Ehcache
CF750F1-思维dp
数据系统分区设计 - 分区再平衡(rebalancing)
Solve the timeout of dbeaver SQL client connection Phoenix query
ML - natural language processing - Introduction to natural language processing
Delayed loading source code analysis:
Flink-1.13.6版本的 Flink sql以yarn session 模式运行,怎么禁用托管
ML - natural language processing - Key Technologies
SQL cultivation manual from scratch - practical part
ML - 语音 - 传统语音模型
The number of query results of maxcompute SQL is limited to 1W
Xcode added mobileprovision certificate file error: Xcode encoded an error
谷歌云盘如何关联Google Colab
Phased summary of the research and development of the "library management system -" borrowing and returning "module