传送门
题目描述
给n点m边,L,s,t 修改m条边中边为0的边, 使满足s,t的最短路长度是L,且输出答案的时候边为0的边的权值必须在[1,1e18]内
分析
感觉这道题细细思考能思考的东西有很多哇
首先我们去把所有可修改的边的权值设为1,然后跑最短路,如果最短路大于l,肯定无解
然后我们设最短路和l的差值为diff,我们要想让终点的最短路增大diff,那么只要将所有点的最短路增大diff就可以了,我们再去做一次最短路,遇到能修改的边,就去修改,更新最短路即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],ne[N],e[N],idx;
int w[N];
int n,m,S,T;
int l;
int diff;
int ds[N],dt[N];
bool st[N];
bool is[N];
void add(int x,int y,int z){
if(!z) is[idx] = true,z = 1;
ne[idx] = h[x],e[idx] = y,w[idx] = z,h[x] = idx++;
}
void spfa(){
queue<int> q;
q.push(S);
memset(ds,0x3f,sizeof ds);
ds[S] = 0;
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t];~i;i = ne[i]){
if(!w[i]) continue;
int j = e[i];
if(ds[j] > ds[t] + w[i]){
ds[j] = ds[t] + w[i];
if(!st[j]){
st[j] = true;
q.push(j);
}
}
}
}
}
void spfa1(){
queue<int> q;
q.push(S);
memset(dt,0x3f,sizeof dt);
dt[S] = 0;
while(q.size()){
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t];~i;i = ne[i]){
int j = e[i];
// if(!w[i]) w[i] = w[i ^ 1] = max(1ll,l - ds[j] - dt[t]);
if(is[i] && ds[j] + diff > dt[t] + w[i]) w[i] = w[i ^ 1] = ds[j] + diff - dt[t];
if(dt[j] > dt[t] + w[i]){
dt[j] = dt[t] + w[i];
if(!st[j]){
st[j] = true;
q.push(j);
}
}
}
}
}
int main() {
read(n),read(m),read(l),read(S),read(T);
memset(h,-1,sizeof h);
while(m--){
int x,y;
ll z;
read(x),read(y),read(z);
add(x,y,z),add(y,x,z);
}
spfa();
if(ds[T] > l){
puts("NO");
return 0;
}
diff = l - ds[T];
spfa1();
if(dt[T] != l){
puts("NO");
return 0;
}
puts("YES");
for(int i = 0;i < n;i++){
for(int u = h[i];~u;u = ne[u]){
int j = e[u];
if(i <= j) continue;
printf("%d %d %d\n",i,j,w[u]);
}
}
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/