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 edges between them. Each edge of the graph is weighted, each weight is a positive integer.
The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassign positive integer weight to each of the edges which weights were erased, so that the length of the shortest path between vertices s and t in the resulting graph is exactly L. Can you help him?
The first line contains five integers n, m, L, s, t (2 ≤ n ≤ 1000, 1 ≤ m ≤ 10 000, 1 ≤ L ≤ 109, 0 ≤ s, t ≤ n - 1, s ≠ t) — the number of vertices, number of edges, the desired length of shortest path, starting vertex and ending vertex respectively.
Then, m lines describing the edges of the graph follow. i-th of them contains three integers, ui, vi, wi (0 ≤ ui, vi ≤ n - 1, ui ≠ vi, 0 ≤ wi ≤ 109). ui and vi denote the endpoints of the edge and wi denotes its weight. If wi is equal to 0 then the weight of the corresponding edge was erased.
It is guaranteed that there is at most one edge between any pair of vertices.
Print "NO" (without quotes) in the only line if it's not possible to assign the weights in a required way.
Otherwise, print "YES" in the first line. Next m lines should contain the edges of the resulting graph, with weights assigned to edges which weights were erased. i-th of them should contain three integers ui, vi and wi, denoting an edge between vertices ui and vi of weight wi. The edges of the new graph must coincide with the ones in the graph from the input. The weights that were not erased must remain unchanged whereas the new weights can be any positive integer not exceeding 1018.
The order of the edges in the output doesn't matter. The length of the shortest path between s and t must be equal to L.
If there are multiple solutions, print any of them.
5 5 13 0 4
0 1 5
2 1 2
3 2 3
1 4 0
4 3 4
YES
0 1 5
2 1 2
3 2 3
1 4 8
4 3 4
2 1 123456789 0 1
0 1 0
YES
0 1 123456789
2 1 999999999 1 0
0 1 1000000000
NO
Here's how the graph in the first sample case looks like :
In the first sample case, there is only one missing edge weight. Placing the weight of 8 gives a shortest path from 0 to 4 of length 13.
In the second sample case, there is only a single edge. Clearly, the only way is to replace the missing weight with 123456789.
In the last sample case, there is no weights to assign but the length of the shortest path doesn't match the required value, so the answer is "NO".
正解:dijkstra
解题报告:
其实这道题也很简单,考场上没想出来真是我傻...
首先那些可以修改的边最小边权为1,则全部改为1,那么如果此时最短路小于等于L,那么至少是有解的。否则无解。
然后依次修改当前还差的边权,直到合法,反正CF机子快,丝毫不虚。
然而我WA了一个晚上,因为我的inf设大了,一加就炸,调了很久才发现...
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const int MOD = ;
int n,m,L,s,t,ecnt;
int first[MAXN],to[MAXM],next[MAXM],w[MAXM],dis[MAXN];
int tag[MAXM];//!!!!!
int dui[MAXN*],head,tail;
bool in[MAXN],ok; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void SPFA(){
//memset(dis,63,sizeof(dis));
for(int i=;i<=n;i++) dis[i]=;//不能设太大!!!不然会炸,WA了好久!!!
dis[s]=; head=tail=; dui[++tail]=s; memset(in,,sizeof(in)); in[s]=;
while(head!=tail) {
head%=n; head++; int u=dui[head]; in[u]=;
for(int i=first[u];i>;i=next[i]) {
int v=to[i];
if(dis[v]>dis[u]+w[i]) {
dis[v]=dis[u]+w[i];
if(!in[v]) { tail%=n; tail++; dui[tail]=v; in[v]=; }
}
}
}
} inline void work(){
//memset(first,-1,sizeof(first));
n=getint(); m=getint(); L=getint(); s=getint()+; t=getint()+;
int x,y,z;ecnt=;
for(int i=;i<=m;i++) {
x=getint()+; y=getint()+; z=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z; if(z==) tag[ecnt]=,w[ecnt]=;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; w[ecnt]=z; if(z==) tag[ecnt]=,w[ecnt]=;
}
SPFA(); if(dis[t]>L) { printf("NO"); return ; }
ok=false;
for(int i=;i<=n;i++) {
for(int j=first[i];j>;j=next[j]) {
if(j & )
if(tag[j]) {
if(dis[t]==L){ ok=true ; break;}
if(dis[t]<L) {
w[j]=w[j^]=L-dis[t]+;
SPFA();
}
}
}
if(ok) break;
}
if(!ok && dis[t]!=L) { printf("NO"); }
else{
printf("YES\n");
for(int i=;i<=n;i++)
for(int j=first[i];j>;j=next[j])
if(j&)
printf("%d %d %d\n",to[j]-,to[j^]-,w[j]);
}
} int main()
{
work();
return ;
}
codeforces 715B:Complete The Graph的更多相关文章
- 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 ...
- Codeforces 1009D:Relatively Prime Graph
D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 【codeforces 716D】Complete The Graph
[题目链接]:http://codeforces.com/problemset/problem/716/D [题意] 给你一张图; 这张图上有一些边的权值未知; 让你确定这些权值(改成一个正整数) 使 ...
- CodeForces 715B Complete The Graph 特殊的dijkstra
Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...
- CF715B. Complete The Graph
CF715B. Complete The Graph 题意: 给一张 n 个点,m 条边的无向图,要求设定一些边的边权 使得所有边权都是正整数,最终 S 到 T 的最短路为 L 1 ≤ n ≤ 100 ...
- 算法:图(Graph)的遍历、最小生成树和拓扑排序
背景 不同的数据结构有不同的用途,像:数组.链表.队列.栈多数是用来做为基本的工具使用,二叉树多用来作为已排序元素列表的存储,B 树用在存储中,本文介绍的 Graph 多数是为了解决现实问题(说到底, ...
- 译:Local Spectral Graph Convolution for Point Set Feature Learning-用于点集特征学习的局部谱图卷积
标题:Local Spectral Graph Convolution for Point Set Feature Learning 作者:Chu Wang, Babak Samari, Kaleem ...
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
随机推荐
- IL查看委托
查看委托的IL 通过IL来查看委托的原理, 委托示例代码 写一个委托的类如下 using System; namespace MyCollection { //定义一个类,该类包含两个静态方法 c ...
- Thread锁 Monitor类、Lock关键字和Mutex类
Monitor 类锁定一个对象 当多线程公用一个对象时,也会出现和公用代码类似的问题,这种问题就不应该使用lock关键字了,这里需要用到System.Threading中的一个类Monitor,我们可 ...
- Web知识总结
一)window.location.href和window.location.replace的区别 1.window.location.href=“url”:改变url地址: 2.window.loc ...
- C语言 结构体中属性的偏移量计算
//计算结构体偏移量 #include<stdio.h> #include<stdlib.h> #include<string.h> //详解:对于offscfof ...
- Android开发环境搭建及常见问题解决方法
转自: http://www.cnblogs.com/rwxwsblog/p/4769785.html 在移动互联网的时代,Android的份额早已超过了苹果.Android的出现无疑加速了移动互联网 ...
- Volly框架的使用基础版及使用中的一些坑 Ace 网络篇(三)
直接把注释粘过来: * Volley使用讲解: * 要实现网络数据请求主要要记住下面三步骤: * 1.创建RequestQueue对象 * 2.创建XXRequest对象(XX代表String,JSO ...
- qt中文乱码问题
首先,声明一下,QString 是不存在中文支持问题的,很多人遇到问题,并不是本身 QString 的问题,而是没有将自己希望的字符串正确赋给QString. 很简单的问题,"我是中文&qu ...
- 《Linux内核设计与实现》 Chapter4 读书笔记
<Linux内核设计与实现> Chapter4 读书笔记 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子 ...
- HTML5 文件异步上传 — h5uploader.js
原文地址:http://imziv.com/blog/article/read.htm?id=62 之前写过一篇H5异步文件上传的文章, 但是很多朋友看着我的这个教程还是出现很多问题,文章写的不是很好 ...
- C++ VS2010 声明没有存储类或类型说明符
函数外只能定义全局变量或者对象,而不能执行语句及调用函数.