Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graphtime limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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?
InputThe 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 0then the weight of the corresponding edge was erased.
It is guaranteed that there is at most one edge between any pair of vertices.
OutputPrint "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.
Examplesinput5 5 13 0 4
0 1 5
2 1 2
3 2 3
1 4 0
4 3 4outputYES
0 1 5
2 1 2
3 2 3
1 4 8
4 3 4input2 1 123456789 0 1
0 1 0outputYES
0 1 123456789input2 1 999999999 1 0
0 1 1000000000outputNONoteHere'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".
题目链接:
http://codeforces.com/contest/715/problem/B
http://codeforces.com/contest/716/problem/D
题目大意:
N个点M条无向边(N<=1000,M<=10000),一个值L,起点S,终点T,M条连接u,v的边,只有边权z为0的要修改为任意不超过1018的正数。
求是否有一种修改方案使得从S到T的最短路恰为L。有则输出YES和每条边修改后的值,没有输出NO。
题目思路:
【最短路】
先从T开始跑最短路,边为0的视为断路。求出每个点x不经过边权为0的边到T的最短路dd[x]。如果dd[S]<L则无解。
接下来从S开始跑最短路,对于当前的now->to,如果当前边权z为0,则修改为L-d[now]-dd[to],如果小于1则必须为1,将新的边权z赋值给原来的边喝它的反向边。
如果d[T]>L则无解。否则即为有解。
(思路是队友告诉我的,这个过程实际是枚举在哪一条边之后没有走0的边,用dijkstra写,每次取出d最小的还未取出的点,开始往下走,我SPFA WA了无数次)
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 1004
#define M 10004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int S,T;
LL L;
int last[N];
LL d[N],dd[N];
bool u[N];
struct xxx
{
int from,to,next;
LL q;
}a[M<<];
bool cmp(int a,int b)
{
return d[a]>d[b];
}
void add(int x,int y,int z)
{
a[++lll].next=last[x];
a[lll].from=x,a[lll].to=y;
a[lll].q=z;
last[x]=lll;
}
void dijkstra(bool f)
{
int i,j,k,now,to;
LL q;
mem(u,);mem(d,);
if(f)d[S]=;
else d[T]=;
for(i=;i<n;i++)
{
for(j=,now=n;j<n;j++)if(!u[j] && d[now]>d[j])now=j;
u[now]=;
for(j=last[now];j;j=a[j].next)
{
to=a[j].to;
if(f)
{
q=a[j].q;
if(!q)q=max(L-d[now]-dd[to],);
a[j].q=a[j^].q=q;
d[to]=min(d[to],d[now]+a[j].q);
}
else if(a[j].q)
d[to]=min(d[to],d[now]+a[j].q);
}
}
}
int main()
{
#ifndef ONLINE_JUDGEW
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
scanf("%d%I64d%d%d",&m,&L,&S,&T);
lll=;mem(last,);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z),add(y,x,z);
}
dijkstra();
if(d[S]<L){puts("NO");continue;}
memcpy(dd,d,sizeof(dd));
dijkstra();
if(d[T]>L){puts("NO");continue;}
puts("YES");
for(i=;i<=m+m;i+=)
printf("%d %d %I64d\n",a[i].from,a[i].to,a[i].q);
}
return ;
}
/*
// //
*/
Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))的更多相关文章
- 【Codeforces】716D Complete The Graph
D. Complete The Graph time limit per test: 4 seconds memory limit per test: 256 megabytes input: sta ...
- 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 ...
- Codeforces Round #372 (Div. 2) A .Crazy Computer/B. Complete the Word
Codeforces Round #372 (Div. 2) 不知不觉自己怎么变的这么水了,几百年前做A.B的水平,现在依旧停留在A.B水平.甚至B题还不会做.难道是带着一种功利性的态度患得患失?总共 ...
- Codeforces Round #372 (Div. 2)
Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph (枚举+最短路)
题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l. 卡了几周的题了,最后还是经群主大大指点……做出来的…… 思路就是跑最短路,然后改权值为最短路和L的差值 ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph
题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...
- Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- C#内存修改
先通过 System.Diagnostics.Process类获取想要编辑的进程 调用API [Flags] public enum ProcessAccessT ...
- .NET 菜单如何链接到指定的框架
这2天我看了网络上很多关于这方面的资料,很多都是抄人家的,要不就是没图说个jiba,要不就是没有说到重点,浪费大家的时间,今天我把我的心得给大家分享下,希望对大家有所帮助. 一.首先,你需要简历一个框 ...
- windows sever 2008 r2 - 限制ip访问
和win 7 旗舰版不同,该操作系统在安装IIS后,非本机的并不能直接访问主机.需要设置主机上的本机的IIS中的IP地址和域限制. 由于我是想在同一个局域网(路由器)中,通过Android操作系统访问 ...
- 监听指定端口数据交互(HttpListenerContext )
很怀念以前做机票的日子,,,,可惜回不去 以前的项目中的,拿来贴贴 场景:同步第三方数据,监听指定地址(指定时间间隔,否则不满足,因为需要处理粘包问题,改篇未实现) 主要内容四个文件:下面分别说下每个 ...
- 请教如何实现UITextField值变化的实时监视
上网搜索以后发现基本的处理方法大概有三种1.KVO方式[textField addObserver:self forKeyPath:@"text" options:0 contex ...
- iOS9适配中出现的一些常见问题
本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...
- Spring 创建bean的时机
默认在启动spring容器的时候,spring容器配置文件中的类就已经创建完成对象了 在<bean>中添加属性lazy-init,默认值为false. true 在c ...
- Spring 实例化bean的方式
实例化bean的方式有三种: 1.用构造器来实例化 2.使用静态工厂方法实例化 3.使用实例工厂方法实例化 当采用构造器来创建bean实例时,Spring对class并没有特殊的要求, 我们通常使用的 ...
- X-Plane数据交互
要用X-Plane进行二次开发,免不了需要进行参数的传递,下面我们来看看与X-Plane进行数据交互都有哪些方式. 与FSX和Flightgear基本一样,X-Plane支持插件,自然也支持通过插件进 ...
- SGU 196.Matrix Multiplication
时间限制:0.25s 空间限制:4M Solution n=10000,m=100000,显然不能用矩阵乘法乘出来. S= ATA 对于矩阵S的一行,所有在A矩阵中1位置的元素都相等,并且都等于这一行 ...