715B Complete The Graph
题目大意
给出一个图,一些边带权,另一些边等待你赋权(最小赋为1).请你找到一种赋权方式,使得 s 到 t 的最短路为 L
n ≤ 1e3 ,m ≤ 1e4 ,L ≤ 1e9
分析
二分所有边的边权和
使得二分后第p条边权值为k,1~p-1条边权值为inf,剩余边权值为1
对于每种情况跑一次最短路
如果结果小于L则增大点权和否则减少
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;
int n,m,s,t,L,d[],vis[];
priority_queue<pair<int,int> >q;
struct node {
int x,y,z;
};
node a[];
int head[],w[],to[],nxt[],cnt;
vector<int>wh;
inline void add(int i){
int x=a[i].x,y=a[i].y,z=a[i].z;
nxt[++cnt]=head[x];
head[x]=cnt;
to[cnt]=y;
w[cnt]=z;
nxt[++cnt]=head[y];
head[y]=cnt;
to[cnt]=x;
w[cnt]=z;
}
inline void dij(){
d[s]=;
q.push(make_pair(,s));
while(!q.empty()){
int x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=;
for(int i=head[x];i;i=nxt[i]){
int y=to[i],z=w[i];
if(d[y]>d[x]+z){
d[y]=d[x]+z;
q.push(make_pair(-d[y],y));
}
}
}
}
inline int ck(int mid){
int i,j,k;
for(i=;i<wh.size();i++){
a[wh[i]].z=+min(mid,inf);
mid-=a[wh[i]].z-;
}
memset(head,,sizeof(head));
memset(w,,sizeof(w));
memset(to,,sizeof(to));
memset(nxt,,sizeof(nxt));
cnt=;
for(i=;i<=m;i++)add(i);
memset(d,0x3f,sizeof(d));
memset(vis,,sizeof(vis));
dij();
return d[t];
}
signed main(){
int i,j,k;
scanf("%lld%lld%lld%lld%lld",&n,&m,&L,&s,&t);
s++,t++;
for(i=;i<=m;i++){
scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].z);
a[i].x++,a[i].y++;
if(!a[i].z)wh.push_back(i);
}
int le=,ri=inf*wh.size();
if(ck(le)>L||ck(ri)<L){
puts("NO");
return ;
}
puts("YES");
while(ri-le>){
int mid=(le+ri)>>;
if(ck(mid)<=L)le=mid;
else ri=mid;
}
ck(le);
for(i=;i<=m;i++)printf("%lld %lld %lld\n",a[i].x-,a[i].y-,a[i].z);
return ;
}

715B Complete The Graph的更多相关文章
- CodeForces 715B Complete The Graph 特殊的dijkstra
Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...
- Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
- 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 ...
- CF715B. Complete The Graph
CF715B. Complete The Graph 题意: 给一张 n 个点,m 条边的无向图,要求设定一些边的边权 使得所有边权都是正整数,最终 S 到 T 的最短路为 L 1 ≤ n ≤ 100 ...
- 【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. 1) B. Complete The Graph (枚举+最短路)
题目就是给你一个图,图中部分边没有赋权值,要求你把无权的边赋值,使得s->t的最短路为l. 卡了几周的题了,最后还是经群主大大指点……做出来的…… 思路就是跑最短路,然后改权值为最短路和L的差值 ...
- Codeforces715 B. Complete The Graph
传送门:>Here< 题意:给出一张带权无向图,其中有一些边权为0.要求将边权为0的边的边权重置为一个任意的正整数,使得从S到T的最短路为L.判断是否存在这种方案,如果存在输出任意一种 解 ...
- Codeforces Round #372 (Div. 1) B. Complete The Graph
题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...
随机推荐
- 魔法效果——dijkstra+堆(邻接表存储)
dijkstra本身每次要for一遍,才能找出最小的节点,但用了堆之后,直接取出堆首就可以了. 但要注意的一点是,c++自带的stl里的priority_queue本身是先入大出的,而我们要求的是最小 ...
- nyoj-115-城市平乱(dijkstra算法)
题目链接 /* Name:nyoj-115-城市平乱 Copyright: Author: Date: 2018/4/25 17:28:06 Description: dijkstra模板题 枚举从 ...
- brew安装php和扩展
brew install homebrew/php/php56 --with-apache 报错: checking if the location of ZLIB install directory ...
- 利用HTML5开发Android笔记(上篇)
资源来自于www.mhtml5.com 杨丰盛老师成都场的PPT分享 一个很简明的demo 可以作为入门基础 学习的过程中做了点笔记 整理如下 虽然内容比较简单 但是数量还是比较多的 所以分了3篇 ( ...
- HDFS超租约异常总结(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException)
HDFS超租约异常总结(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException) 转载 2014年02月22日 14:40:58 96 ...
- bzoj 3160 万径人踪灭——FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3160 似乎理解加深了. 用卷积算相同的位置:先把 a 赋成1. b 赋成0,卷积一遍:再把 ...
- mysql下this is incompatible with sql_mode=only_full_group_by解决方案
本地测试没有问题,部署到客户服务器之后报如下错误: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 o ...
- Linux内核 - 定时器
#include <linux/timer.h> //头文件 struct timer_list mytimer; //定义变量 static void my_timer(unsigned ...
- LTE流程
LTE 过程全流程 1. UE处于关闭状态 2. 打开UE电源. 3. 搜索附近的频率 4. 同步时间 5. 小区搜索 6. 小区选择 7. 解码MIB 8. 解码SIB 9. 初始化RACH过程 1 ...
- GUI练习中
总结:JFrame和Frame是有很大差别的. 不要混淆.否则方法是不能成功调用的 特别是背景色:JFrame.对象f在main里无法调用背景色前景色都不想显示 一下是书上的一段代码,编译错误,但是可 ...