传送门:>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. jQuery 初识 筛选器 属性选择器

    ---------------------------大事使我们惊讶,小事使我们沮丧,久而久之,我们对这二者都会习以为常. 一 jQuery是什么? [1]   jQuery由美国人John Resi ...

  2. java----牛客练习

    1. 形式参数就是函数定义时设定的参数.例如函数头 int min(int x,int y,int z) 中 x,y,z 就是形参.实际参数是调用函数时所使用的实际的参数.   真正被传递的是实参   ...

  3. hybrid App cordova打包webapp PhoneGap

    Hybrid APP基础篇(一)->什么是Hybrid App APP三种开发模式--之--HybridApp解决方案 Hybrid App开发 四大主流平台分析 Hybrid App 开发模式 ...

  4. Could not open connection

    意思是不能打开JDBC连接,如果代码没写错的话就是服务没打开,开一下服务就行了,oracle两个必开的服务:OracleServiceORCL和OracleOraDb11g_home2TNSListe ...

  5. 一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录

    1.如果A表TID是自增长,并且是连续的,B表的ID为索引 select * from a,b where a.tid = b.id and a.tid>500000 limit 200; 2. ...

  6. Spring配置文件beans.xml头部配置解释

    Spring配置文件beans.xml头部配置解释 - EasonJim - 博客园https://www.cnblogs.com/EasonJim/p/6880329.html

  7. Sublime Text3 配置 NodeJs 开发环境

    题外话:使用visual studio开发NodeJs也是很方便,只需要安装插件即可. 本着对Sublime Text3的喜爱,尤其是最近更新后,界面和功能上感觉更nice了,那就配置一发环境吧! ( ...

  8. druid 连接Oracle时出现的错误

    转博主https://blog.csdn.net/jiangyu1013/article/details/70237550#commentsedit mysql 更新 SQL 语句 无错误 批量 报错 ...

  9. 关于 ajax

    1.type 提交类型 get /post 2.async 默认true 异步 3.cache 默认 true 读取缓存 false不读取缓存  会在请求后面 添加一个时间戳 https://www. ...

  10. 版本控制--git+idea