CodeForces 715B Complete The Graph 特殊的dijkstra
题解:
比较特殊的dij的题目。
dis[x][y] 代表的是用了x条特殊边, y点的距离是多少。
然后我们通过dij更新dis数组。
然后在跑的时候,把特殊边都先当做1在跑,并且经过特殊边的时候,记得将x更新。
然后如果dis[0][t] < L 则代表不用特殊边也会小于L。所以无论是特殊的边答案是多少,dis[0][t]<L也是固定的。
然后我们不断检查dis[c][t] (for c 1 to N) 是不是 <= L 。
找到对应的dis[c][t]之后, 把最短路上的特殊边更新成符合答案的值。 其他特殊边更新成L+1。
代码从学长那里学的, 可以说十分像了。
对我来说比较新颖的是, 把边初始值赋值为L+1,这样就可以在int的范围内跑完全程的dij了, 毕竟如果路径长度 > L 之后就没必要再跑了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll; const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e3 + ;
const int M = 2e4 + ;
int n, m, L, s, t;
int head[N], to[M], val[M], nt[M], tot;
void add(int u, int v, int w){
to[tot] = v;
val[tot] = w;
nt[tot] = head[u];
head[u] = tot++;
}
int dis[N][N];
pll pre[N][N];
typedef tuple<int,int,int> tup;
priority_queue<tup, vector<tup>, greater<tup> > pq;
bool dij(){
for(int i = ; i < N; ++i)
fill(dis[i], dis[i] + N, L+);
dis[][s] = ;
pq.push(tup(,,s));
/// dis , 0-edge, now-point
while(!pq.empty()){
int dd, c, u;
tie(dd, c, u) = pq.top();
pq.pop();
if(dd != dis[c][u]) continue;
for(int i = head[u]; ~i; i = nt[i]){
int v = to[i];
int w = dd + val[i] + (!val[i]);
int nc = c + (!val[i]);
if(nc >= N) continue;
if(dis[nc][v] > w){
dis[nc][v] = w;
pq.push(tup(w, nc, v));
pre[nc][v] = pll(u, i);
}
}
}
if(dis[][t] < L) return false;
if(dis[][t] == L) return true;
for(int c = ; c < N; ++c){
if(dis[c][t] <= L){
int add = L - dis[c][t];
int cc = c, now = t;
while(now != s){
pll tt = pre[cc][now];
now = tt.fi;
if(val[tt.se] == ){
val[tt.se] = val[tt.se^] = add + ;
add = ;
cc--;
}
}
return true;
}
}
return false;
}
int main(){
memset(head, -, sizeof head);
scanf("%d%d%d%d%d", &n, &m, &L, &s, &t);
int u, v, w;
for(int i = ; i <= m; ++i){
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
if(dij()){
puts("YES");
for(int i = ; i < tot; i += ){
if(val[i] == ) val[i] = L + ;
printf("%d %d %d\n", to[i], to[i^], val[i]);
}
}
else puts("NO");
return ;
}
CodeForces 715B Complete The Graph 特殊的dijkstra的更多相关文章
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
- 715B Complete The Graph
传送门 题目大意 给出一个图,一些边带权,另一些边等待你赋权(最小赋为1).请你找到一种赋权方式,使得 s 到 t 的最短路为 L n ≤ 1e3 ,m ≤ 1e4 ,L ≤ 1e9 分析 二分所有边 ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- 【Codeforces】716D Complete The Graph
D. Complete The Graph time limit per test: 4 seconds memory limit per test: 256 megabytes input: sta ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- CF715B. Complete The Graph
CF715B. Complete The Graph 题意: 给一张 n 个点,m 条边的无向图,要求设定一些边的边权 使得所有边权都是正整数,最终 S 到 T 的最短路为 L 1 ≤ n ≤ 100 ...
- codeforces 372 Complete the Word(双指针)
codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...
- codeforces 715B:Complete The Graph
Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...
随机推荐
- Java秒杀系统实战系列~商品秒杀代码实战
摘要: 本篇博文是“Java秒杀系统实战系列文章”的第六篇,本篇博文我们将进入整个秒杀系统核心功能模块的代码开发,即“商品秒杀”功能模块的代码实战. 内容: “商品秒杀”功能模块是建立在“商品详情”功 ...
- java学习笔记(中级篇)—java实现高质量图片压缩
使用java几十行代码实现一个高质量图片压缩程序,再也不用去自己找网络的压缩程序啦!而且很多网上的工具还有水印或者其他的限制,自己动手写一个简单的应用,是再合适不过了. 一.实现原理 1.声明两个字符 ...
- 【TCP/IP】ICMP协议
ICMP协议有两种报文: 1,查询报文 2,差错报文
- Java +支付宝 +接入+最全+最佳-实战-demo
一.支付宝配置: 1.需要在支付宝商户平台购买支付的产品并开通支付. 2.购买支付产品登录支付宝:https://auth.alipay.com/login/index.htm 3.登录之后首页点击查 ...
- react学习(一)--JSX简介
由于在中国银联实习的项目要用到react,所以不得不硬着头皮把react学习一下.这是要往全栈发展吗0.0 正文: 一个最简单的React例子如下, ReactDOM.render( <h1&g ...
- RE最全面的正则表达式----字符验证
二.校验字符的表达式汉字:^[一-彪]{0,}$英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$长度为3-20的所有字符:^.{3,20}$由26个英文字母组成的字 ...
- Python 学习笔记(6)— 字符串格式化
字符串格式化处理 远古写法 以前通常使用运算符号 % ,%s 插入的值 String 类型,%.3f 指插入的值为包含 3 位小数的浮点数: format1 = "%s, %s!" ...
- Android Pie 私人 DNS 使用教程
本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章 ...
- Gin + Vue全栈开发实战(二)
尝试地写了第一篇自己学习Go Web框架的感受和入门的文章,发现反响还不错,大家也提出了很多的问题来一起交流.近期也渐渐地出现了很多有关go语言开发的相关文章,包括有在蚂蚁金服的大牛的分享,我也一直有 ...
- CentOS 安装 JDK 三种形式详细总结
一.下载 JDK 点击下载:jdk-8u211-linux-x64.tar.gz 根据需要选择对应版本和位数,并将文件放入CentOS中的相关目录中,以 /java/jdk 目录为例,执行 m ...