传送门:>Here<

题意:给出一张带权无向图,其中有一些边权为0。要求将边权为0的边的边权重置为一个任意的正整数,使得从S到T的最短路为L。判断是否存在这种方案,如果存在输出任意一种

解题思路

  注意是最短路是L,而非存在一条路径为L。并且边权为0的边必须变为正整数,最小也得是1

  这题由于n=1000,所以可以稍微暴力一点……

  首先,先不加任何一条为0的边跑Dij,如果此时的最短路已经$< L$,那么后面的边无论怎么加都不会使最短路比当前的大了,因此无解

  此时最短路$\geq L$。然后考虑一条一条把边加进图里。每一条塞进图里的边权值都设为最小(也就是1)。如果加上了当前这条边使得最短路$< L$了,那么导致最短路变小的一定就是当前这一条边。因为除了通过当前这条边的路径以外其他路径都$\geq L$。所以我们可以修改这一条边的权值为$L-d[t]+1$,也就是把最短路凑成等于L。并且修改之后所有的路径依然$\geq L$。如果加上当前这一条边最短路依然$\geq L$,那么这条边就是废掉的,可以不管。所以我们需要做的,就是对于每一条使最短路$< L$的边都做此修改。最后判断最短路是否等于L即可。复杂度$O(m^2\ log\ n)$,非常巧妙~

Code

  注意应当把无法使路径变小的边权值设为1

/*By DennyQi*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
#define ERR {puts("NO");return 0;}
using namespace std;
typedef long long ll;
#define int long long
const int MAXN = ;
const int MAXM = ;
const int INF = 1e13;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar(); return x * w;
}
struct Edge{
int u,v,w;
}a[MAXM];
struct Node{
int idx,w;
};
inline bool operator < (const Node& a, const Node& b){
return a.w > b.w;
}
int N,M,L,S,T,x,y,z;
int first[MAXM*],nxt[MAXM*],to[MAXM*],cost[MAXM*],num_edge;
priority_queue <Node> q;
int d[MAXN],vis[MAXN];
inline void add(int u, int v, int w){
to[++num_edge] = v;
cost[num_edge] = w;
nxt[num_edge] = first[u];
first[u] = num_edge;
}
inline void Dijkstra(int s){
for(int i = ; i <= N; ++i) d[i] = INF;
memset(vis, , sizeof(vis));
d[s] = ;
q.push((Node){s, });
int u, v;
while(!q.empty()){
u = q.top().idx; q.pop();
if(vis[u]) continue; vis[u]=;
for(int i = first[u]; i; i = nxt[i]){
v = to[i];
if(d[u] + cost[i] < d[v]){
d[v] = d[u] + cost[i];
q.push((Node){v,d[v]});
}
}
}
}
main(){
// freopen(".in","r",stdin);
N=r,M=r,L=r,S=r,T=r;
for(int i = ; i <= M; ++i){
a[i].u=r, a[i].v=r, a[i].w=r;
if(a[i].w != ){
add(a[i].u,a[i].v,a[i].w);
add(a[i].v,a[i].u,a[i].w);
}
}
Dijkstra(S);
if(d[T] < L) ERR;
for(int i = ; i <= M; ++i){
if(a[i].w != ) continue;
a[i].w = ;
add(a[i].u,a[i].v,);
add(a[i].v,a[i].u,);
Dijkstra(S);
if(d[T] < L){
a[i].w = - d[T] + L;
cost[num_edge-] = - d[T] + L;
cost[num_edge] = - d[T] + L;
}
}
/* for(int i = 1; i <= M; ++i){
printf("%d %d %d\n", a[i].u,a[i].v,a[i].w);
}
printf("d[%d] = %d\n",T,d[T]);*/
Dijkstra(S);
if(d[T] != L) ERR;
puts("YES");
for(int i = ; i <= M; ++i){
printf("%lld %lld %lld\n", a[i].u,a[i].v,a[i].w);
}
return ;
}

Codeforces715 B. Complete The Graph的更多相关文章

  1. 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 ...

  2. CF715B. Complete The Graph

    CF715B. Complete The Graph 题意: 给一张 n 个点,m 条边的无向图,要求设定一些边的边权 使得所有边权都是正整数,最终 S 到 T 的最短路为 L 1 ≤ n ≤ 100 ...

  3. 【Codeforces】716D Complete The Graph

    D. Complete The Graph time limit per test: 4 seconds memory limit per test: 256 megabytes input: sta ...

  4. CodeForces 715B Complete The Graph 特殊的dijkstra

    Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...

  5. 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 ...

  6. Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...

  7. Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)

    题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l. 卡了几周的题了,最后还是经群主大大指点……做出来的…… 思路就是跑最短路,然后改权值为最短路和L的差值 ...

  8. Codeforces Round #372 (Div. 1) B. Complete The Graph

    题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...

  9. 715B Complete The Graph

    传送门 题目大意 给出一个图,一些边带权,另一些边等待你赋权(最小赋为1).请你找到一种赋权方式,使得 s 到 t 的最短路为 L n ≤ 1e3 ,m ≤ 1e4 ,L ≤ 1e9 分析 二分所有边 ...

随机推荐

  1. vue的高阶组件

    探索Vue高阶组件 探索Vue高阶组件的使用 Vue高阶组件的使用方法 高阶组件应用-组件重新实例化 深入理解React 高阶组件 探索Vue高阶组件 2018-01-05 探索Vue高阶组件 Vue ...

  2. Dubbo负载均衡与集群容错机制

    1  Dubbo简介 Dubbo是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现. 作为一个轻量级RPC框架,D ...

  3. Vue之小入门

    Vue之小入门 <div id="app">{{ greeting }}</div> <script> let oDiv = document. ...

  4. 【问题解决方案】Dev C++ 无法调试的问题与解决

    听翁恺老师课的时候用到一个叫DevC++的编辑器. 学到调试部分的时候,老师的没问题我的报错.我?? 试一试网上查到的方法: 工具 --> 编译选项 --> 代码生成/优化 --> ...

  5. Handling duplicate form submission in Spring MVC

    javaweb开发之防止表单重复提交 - u012843873的博客 - CSDN博客 https://blog.csdn.net/u012843873/article/details/5526212 ...

  6. Thread类相关方法

    线程对象 每一个线程都是和类Thread的实例相关联的.在Java中,有两种基本的使用Thread对象的方式,可用来创建并发性程序.  1.在应用程序需要发起异步任务的时候,只要生成一个Thread对 ...

  7. 临时的ThisCall

    // 获取当前定位 changeCity: function () { let that = this; that.locationClose(); Upj._changeCity().then((d ...

  8. Python 基础知识----流程控制

    判断语句 循环语句 嵌套

  9. 【git】git add 添加错文件 撤销

     git add 添加 多余文件 这样的错误是由于, 有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操 ...

  10. python之路--MySQL数据库初识

    一 . MySQL安装 # 下载MySQL地址 https://dev.mysql.com/downloads # 要选稳定的,不要选最新的,稳定的就是半年以上没有出现过bug 现在5.6.43为绝大 ...