[Codeforces Round #372 DIV1B (CF715B)] Complete The Graph

时间:2022-04-30 23:21:41

题意

给定一个图,图中一些边的长度未知,问是否存在最短路长度恰好为 L 的可能

题解

将所有未知边长度赋为 1 ,最短路长度为 lenmin ,将所有未知边长度赋为无穷,最短路长度为 lenmax 。如果 lenminLlenmax ,则有解。
二分边长,使得未知边长度为 k 时最短路长度 lenk<L ,未知边长度为 k+1 时最短路长度 lenk>L
此时,我们使所有未知边长度为 k ,然后逐一将这些边权变为 k+1 ,最短路长度一定会逐渐增长至 L 。所以可以将所有的未知边塞到数组 [1...m] 里,二分一个中间位置 pos ,使得 [1,pos] 长度为 k+1 [pos+1,m] 长度为 k 时,最短路长度为 L

代码

/// by ztx
/** * Bisect to find the path len(A) < L < len(B) * continue bisecting to find a set of unknow edges to be length A * and the other edges of unknow edges to be length B */

#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define rep(i,l,r) for(i=(l);i< (r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
#define rev(i,r,l) for(i=(r);i> (l);i--)
typedef long long ll ;
typedef double lf ;
int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
    ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
    if (CH == '-') NEG = true , CH = getchar() ;
    while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
    if (NEG) ret = -ret ;
}
template <typename TP>inline void readc(TP& ret) {
    while (ret=getchar() , ret<'!') ;
    while (CH=getchar() , CH>'!') ;
}
template <typename TP>inline void reads(TP *ret) {
    ret[0]=0;while (CH=getchar() , CH<'!') ;
    while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ;
    ret[ret[0]+1]=0;
}

#include <deque>
#include <cstring>

#define maxn 1010LL
#define maxe 10010LL
#define infi 0x3f3f3f3fLL

#define to(p) e[0][p]
#define val(p) e[1][p]
#define nxt(p) e[2][p]
int e[3][maxe<<1] = {0}, star[maxn] = {0}, tote = 1, rec[maxe] = {0}, totr = 0;
bool reced[maxe<<1] = {0};
inline void AddEdge(int u,int v,int w)
{   tote ++ , to(tote) = v, val(tote) = w, nxt(tote) = star[u], star[u] = tote;
    tote ++ , to(tote) = u, val(tote) = w, nxt(tote) = star[v], star[v] = tote; }
inline void Modify(int w)
{   int i; Rep (i,1,totr) val(rec[i]) = w, val(rec[i]^1) = w; }

int n, S, T;

std::deque<int>q;
bool inq[maxn] = {0};
int dis[maxn];
#define pb push_back
#define pf push_front
#define pop pop_front
inline int SPFA() {
    int u, v, p;
    memset(dis,0x3f,sizeof dis);
    dis[S] = 0, q.pb(S);
    while (!q.empty())
        for (u=q.front(),q.pop(),inq[u]=false,p=star[u];v=to(p),p;p=nxt(p))
            if (dis[v] > dis[u]+val(p))
                if (dis[v]=dis[u]+val(p), !inq[v])
                    if (inq[v]=true, !q.empty()&&dis[q.front()]>dis[v]) q.pf(v);
                    else q.pb(v);
    return dis[T];
}

int main() {
    int m, exp, i, u, v, w, L, R, M;
    read(n), read(m), read(exp), read(S), read(T), S ++ , T ++ ;
    rep (i,0,m) {
        read(u), read(v), read(w), u ++ , v ++ ;
        if (w) AddEdge(u,v,w);
        else AddEdge(u,v,1), rec[++totr] = tote, reced[tote] = true, reced[tote^1] = true;
    }
    u = SPFA();
    if (u > exp) { puts("NO"); goto END; }
    if (u == exp) goto ANS;
    Modify(infi); v = SPFA();
    if (v < exp) { puts("NO"); goto END; }
    if (v == exp) goto ANS;
    L = 1, R = 1000000000;
    while (R-L > 1) // (L,R] SPFA(L) < exp SPFA(R) >= exp
        if (Modify(M = (L+R)/2), SPFA() < exp) L = M; else R = M;
    Modify(R);
    if (SPFA() == exp) goto ANS;
    w = R; L = 1, R = totr;
    while (R-L > 1) { // (L,R] L < exp R >= exp
        M = (L+R)/2;
        rep (i,1,M) val(rec[i]) = w, val(rec[i]^1) = w;
        Rep (i,M,totr) val(rec[i]) = w-1, val(rec[i]^1) = w-1;
        if (SPFA() < exp) L = M; else R = M;
    }
    rep (i,1,R) val(rec[i]) = w, val(rec[i]^1) = w;
    Rep (i,R,totr) val(rec[i]) = w-1, val(rec[i]^1) = w-1;
    ANS:;
    puts("YES");
    for (i = 2; i < tote; i += 2)
        printf("%d %d %d\n", to(i^1)-1, to(i)-1, val(i));
    END:;
getchar();
    return 0;
}