Codeforces 716D D. Complete The Graph

本文介绍了一个编程问题:如何为图中被擦除的边赋予权重,使得从起点到终点的最短路径恰好等于给定长度。文章提供了一段代码示例,通过示例输入输出展示了问题解决的过程。

D. Complete The Graph
time 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?

Input
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.

Output
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.

Examples
input
5 5 13 0 4
0 1 5
2 1 2
3 2 3
1 4 0
4 3 4
output
YES
0 1 5
2 1 2
3 2 3
1 4 8
4 3 4
input
2 1 123456789 0 1
0 1 0
output
YES
0 1 123456789
input
2 1 999999999 1 0
0 1 1000000000
output
NO
Note
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”.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define I64d_OJ

#ifdef I64d_OJ
#define LL __int64
#endif // I64d_OJ
//#ifdef I64d_OJ
//#define LL long long
//#endif // I64d_OJ

typedef unsigned LL ULL;
typedef unsigned uint;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<LL,LL> pLL;
typedef vector<int> vi;

inline LL read(){
    LL x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-f; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
#define MID(a,b) (((LL)(a)+(LL)(b))>>1)
#define absx(a) ((a)<0?-(a):(a))
#define MK(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define lowbit(x) ((x)&-(x))

template< typename T >
inline void Max(T &a,T &b){if(a<b) a=b;}
template< typename T>
inline void Min(T &a,T &b){if(a>b) a=b;}

const double pi=(double)acos(-1.0);
const double eps=(double)1e-10;
const int INF= (int) 0x3f3f3f3f;
const int MOD = (int) 1e9+7;
const int MAXN =(int) 1e4+10;
const int MAXM = (int) 1e4+10;

///--------------------START-------------------------
int n,m,s,t;
LL L;
LL dist[MAXN];
//int cnt[MAXN];
//int p[MAXN];
bool vis[MAXN];
int mark[MAXN];

struct Edge{
    int u,v;
    LL w;
    int next;
}edge[MAXM<<1];
int head[MAXN];
int edgenum;

void Add_Edge(int u,int v,LL w){
    edge[edgenum].u=u;
    edge[edgenum].v=v;
    edge[edgenum].w=w;
    edge[edgenum].next=head[u];
    head[u]=edgenum++;
}

void SPFA_BFS(int s){
    queue<int> q;
    for(int i=0;i<MAXN;i++) dist[i]=INF;
    dist[s]=0;
//    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));

    q.push(s);
    vis[s]=1;
//    cnt[s]=1;
    while(!q.empty()){
        int u=q.front(); q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            LL w=edge[i].w;
            if(dist[u]+w<dist[v]){
                dist[v]=dist[u]+w;
                if(!vis[v]){
                    vis[v]=1;
//                    cnt[v]++;
                    q.push(v);
                }
            }
        }
    }
}

void work(){
    scanf("%d%d%I64d%d%d",&n,&m,&L,&s,&t);
    memset(head,-1,sizeof(head));
    edgenum=0;
    for(int i=0;i<m;i++){
        int u,v;
        LL w;
        scanf("%d%d%I64d",&u,&v,&w);
        if(w==0){w=1; mark[i]=1;}
        Add_Edge(u,v,w);
        Add_Edge(v,u,w);
    }
    SPFA_BFS(s);
    if(dist[t]==INF||dist[t]>L){printf("NO\n"); return;}
    LL delta=L-dist[t];
    int ok=0;
    if(dist[t]==L) ok=1;
    if(!ok){
        for(int i=0;i<m;i++){
            if(mark[i]){
                edge[i*2].w+=delta;
                edge[i*2+1].w+=delta;
                SPFA_BFS(s);
                if(dist[t]==L){ok=1; break;}
                else delta=L-dist[t];
            }
        }
    }
    if(!ok) printf("NO\n");
    else{
        printf("YES\n");
        for(int i=0;i<m;i++){
            printf("%d %d %I64d\n",edge[i*2].u,edge[i*2].v,edge[i*2].w);
        }
    }
}

///--------------------END-------------------------
int main(){
#ifdef NNever
freopen("data.in","r",stdin);
///freopen("out.txt","w",stdout);
#endif // NNever
    work();
    return 0;
}
对于Codeforces Round 1005 Div. 2中的D题解答或解释,当前提供的引用资料并未直接涉及该轮次的比赛题目详情。然而,可以基于过往比赛的经验以及相似类型的编程竞赛问题提供一般性的指导。 ### 解决方案概述 通常情况下,在解决此类算法竞赛题目时,会遵循特定的方法论来处理输入数据并计算所需的结果。虽然具体到Codeforces Round 1005 Div. 2 Problem D的信息未被提及,但可以根据以往经验推测可能涉及到的数据结构和算法技术: - **读取测试案例数量**:程序首先应该能够接收多个独立的测试案例数目\(t\),其中每一个案例都包含了不同的参数集[^3]。 - **解析数组元素**:针对每个测试案例,需解析给定长度为\(n\)的一系列整数\[a_1, a_2,...,a_n\]作为操作对象[^2]。 - **查询次数限制**:需要注意的是,所有测试案例中查询总数不得超过设定的最大值,比如这里提到不超过\(2 \times 10^5\)次查询[^1]。 - **输出格式规定**:当准备打印最终答案时,应按照指定格式输出结果,并继续处理下一个测试案例直到完成全部测试[^4]。 考虑到这些通用原则,如果要设计一个适用于此类型问题的解决方案框架,则可能会如下所示: ```python def solve_problem(): import sys input = sys.stdin.read data = input().split() index = 0 results = [] t = int(data[index]) index += 1 for _ in range(t): n = int(data[index]) index += 1 numbers = list(map(int, data[index:index+n])) index += n # 假设这里是解决问题的核心逻辑部分, # 需要根据具体的Problem D描述调整这部分代码实现。 result_for_case = "Example Result" results.append(result_for_case) print("\n".join(results)) ``` 上述伪代码展示了如何构建基本架构用于批量处理多组测试用例,但是核心业务逻辑需要依据实际问题定义进行填充和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值