CodeForces 716D Complete The Graph (SPFA)

本文介绍了一种解决图中部分边权重缺失的问题的方法。通过尝试为缺失权重的边分配合适的正整数值,使得从指定起点到终点的最短路径长度恰好等于给定的目标值。文章提供了具体的算法实现思路及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Complete The Graph
Time Limit: 4000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u
Submit

Status

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

Sample Input
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
Hint
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”.

Source
Codeforces Round #372 (Div. 2)

看了网上的题解做的。。感觉很暴利。就是把每一条不确定的边都去试一下

#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
#include "queue"
using namespace std;
int head[20005];
long long dis[20005];
int len=0;
int n,m,l,s,t;
bool mark[20005];
struct edge
{
    int to,next,u,v;
    long long weight;
}edge[20005];
void addEdge(int u,int v,long long w)
{
    edge[len].u=u;
    edge[len].v=v;
    edge[len].to=v;
    edge[len].next=head[u];
    edge[len].weight=w;
    head[u]=len;
    len++;
}
void spfa()
{
    queue <int> list;
    list.push(s);
    bool vis[1005];
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f3f3f3f,sizeof(dis));
    dis[s]=0;
    vis[s]=1;
    while(!list.empty())
    {
        int now=list.front();
        list.pop();
        vis[now]=0;
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            int to=edge[i].to;
            long long w=edge[i].weight;
            if(dis[now]+w<dis[to])
            {
                dis[to]=dis[now]+w;
                if(vis[to]==0)
                {
                    vis[to]=1;
                    list.push(to);
                }
            }
        }
    }
}

void gao()
{
    spfa();
    if(dis[t]==0x3f3f3f||dis[t]>l)
    {
        printf("NO\n");
        return;
    }
    long long delta=l-dis[t];
    int flag=0;
    if(dis[t]==l)
        flag=1;
    if(flag==0)
    {
        for(int i=0;i<m;i++)
        {
            if(mark[i])
            {
                edge[i*2].weight+=delta;
                edge[i*2+1].weight+=delta;
                spfa();
                if(dis[t]==l)
                {
                    flag=1;
                    break;
                }
                else
                    delta=l-dis[t];

            }
        }

    }
    if(flag==0)
        printf("NO\n");
    else
    {
        printf("YES\n");
        for(int i=0;i<m;i++)
        {
            printf("%d %d %lld\n",edge[i*2].u,edge[i*2].v,edge[i*2].weight);
        }
    }
}


int main()
{
    while(~scanf("%d%d%d%d%d",&n,&m,&l,&s,&t))
    {
        memset(head,-1,sizeof(head));
        memset(mark,0,sizeof(mark));
        len=0;
        for(int i=0;i<m;i++)
        {
            int u,v;
            long long w;
            scanf("%d%d%lld",&u,&v,&w);
            if(w==0)
            {
                w=1;
                mark[i]=1;
            }
            addEdge(u, v, w);
            addEdge(v, u, w);

        }
        gao();
    }
}
### CodeForces平台上关于SPFA算法的问题和资源 CodeForces作为一个知名的在线编程竞赛平台,提供了丰富的题目库供用户练习不同类型的算法问题。对于寻找特定于SPFA(Shortest Path Faster Algorithm)算法的问题,在该网站上可以通过标签功能来定位相关题目[^1]。 然而需要注意的是,由于SPFA并非像Dijkstra那样被广泛设为独立分类标签,因此可能不会直接找到标记有此名称的题目。建议通过搜索含有图论、最短路径等相关关键词的挑战来进行间接筛选。 另外一种方法是在高级搜索选项中利用自定义过滤器设置难度范围以及涉及的知识点领域,从而提高发现适合练习SPFA技能题目的几率。例如可以尝试访问带有`shortest paths` 或者 `graphs` 标签并且难度等级处于中级到高级之间的题目列表。 ```python import requests from bs4 import BeautifulSoup def find_spfa_problems(): url = "https://codeforces.com/problemset" params = { 'tags': 'data structures, graphs', # Tags that might contain SPFA related problems 'order': 'BY_RATING_ASC' # Order by difficulty ascendingly } response = requests.get(url, params=params) soup = BeautifulSoup(response.text, 'html.parser') problem_list = [] for row in soup.select('table.problems tr'): columns = row.find_all('td') if len(columns) >= 2: name_cell = columns[0].find('a') rating_cell = columns[-1] if name_cell and rating_cell: title = name_cell.get_text(strip=True) link = f"https://codeforces.com{name_cell['href']}" try: rating = int(rating_cell.get_text(strip=True)) problem_list.append((title, link, rating)) except ValueError: continue filtered_problems = [(t,l,r) for t,l,r in problem_list if r>=1700 and r<=1900] return filtered_problems[:5] # Return top five matches as an example print(find_spfa_problems()) ``` 上述Python脚本展示了如何自动化地从CodeForces获取潜在适用于练习SPFA算法的数据结构与图形类别的难题链接。请注意这只是一个示范性的实现方式,并不是所有返回的结果都一定涉及到SPFA;实际应用时还需要人工进一步甄别确认哪些具体问题是基于这一算法设计的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值