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 ...
随机推荐
- -bash: wget: command not found的两种解决方法
今天给服务器安装新环境时,wget 时提示 -bash:wget command not found,很明显没有安装wget软件包.一般linux最小化安装时,wget不会默认被安装,这里是CentO ...
- Linux基于libmemcached,php扩展memcached的安装
安装环境:CentOS 6.4 php的扩展memcache,不支持cas,所以我们要装memcached扩展,memcached扩展是基于libmemcached,所以要先安装libmemcache ...
- PHP简单post验证绕过
if($_POST[user] && $_POST[pass]) { $conn = mysql_connect("*******", "****&quo ...
- isAnimated函数
function isAnimated($obj){ var flag=false; if($obj.is(":animated")){ flag=true; } return f ...
- RDLC系列之六 打印纸张的大小(未解决)
问题: 当报表的宽度大于高度的时候,RDLC就默认成横向打印了,把打印纸竖着放就可以正常打印了,但是如果是针式打印机的话,纸张不能连续打印. 如果把宽度和高度值对调,然后横向放纸,打印机里选择“竖打” ...
- C# 无边框窗体之窗体移动
点击窗体任意位置移动窗体: 需要添加命名空间: using System.Runtime.InteropServices; private const int WM_NCLBUTTONDOWN = 0 ...
- Content Factory:辅助 MonoGame 游戏开发
Content Factory 是一款辅助 MonoGame 游戏开发的工具.它提供素材管理的多项功能,包括编译素材.编辑自定义数据等,并能同时应用多个游戏平台. 项目设置 选择要创建游戏项目的平台, ...
- Apache Thrift
Baidu Thrift Google Thrift Apache Thrift - 可伸缩的跨语言服务开发框架
- OAF 使用 javascript 使某个按钮在5秒内不能重复点击
首先要保证按钮是BUTTON,并且按钮事件设置firePartialAction. public class CuxXXXXPGCO extends OAControllerImpl { public ...
- Ant 执行 YUICompressor
Ant 执行 YUICompressor 任务压缩 JavaScript 和 CSS 文件,解决中文乱码问题,增加源文件字符编码集设定 标签: javascriptantcss任务encodingnu ...