CodeForces 716D Complete The Graph(spfa)+spfa标记最短路径 + 负环判断

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.

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

题目大意

n个顶点m条边的无向图,可以将输入权值为0的边修改为任意值,问是否满足一种情况使得从s到t的最短路等于L。若存在,输出“YES”且输出这样一组数据,否则输出“NO”.

解题思路

建边时将权值不确定的边先设为无穷大,然后判断此时s->t的最短路,若小于L,则直接输出“NO”,若等于L,则直接输出这种情况,否则尝试修改每一条未知边,判断是否符合条件。

代码实现

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
#define INF 0x3f3f3f3f
struct node
{
    int u;
    int v;
    ll cost;
    int next;
}edge[maxn*2];
int head[maxn],tot;
int vis[maxn],dis[maxn];
int s,t;
vector<int>vv;
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
    vv.clear();
}
void addedge(int u,int v,int c)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].cost=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void out()
{
    cout<<"YES"<<endl;
    for(int i=0;i<tot;i+=2)
        cout<<edge[i].u<<" "<<edge[i].v<<" "<<edge[i].cost<<endl;
}
ll spfa()
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    vis[s]=1;
    queue<int>qu;
    qu.push(s);
    while(!qu.empty())
    {
        int u=qu.front();
        qu.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                if(!vis[v])
                {
                    vis[v]=1;
                    qu.push(v);
                }
            }
        }
    }
    return dis[t];
}
int main()
{
    int n,m,L;
    int u,v,c;
    cin>>n>>m>>L>>s>>t;
    init();
    for(int i=0;i<m;i++)
    {
        cin>>u>>v>>c;
        if(c==0)
        {
            c=INF;
            vv.push_back(tot);
        }
        addedge(u,v,c);
        addedge(v,u,c);
    }
    ll min_cost=spfa();
    if(min_cost<L)
        cout<<"NO"<<endl;
    else if(min_cost==L)
        out();
    else
    {
        bool flag=false;
        for(int i=0;i<(int)vv.size();i++)
        {
            int temp=vv[i];
            edge[temp].cost=edge[temp+1].cost=1;
            min_cost=spfa();
            if(min_cost<=L)
            {
                edge[temp].cost=edge[temp+1].cost=L-min_cost+1;
                out();
                flag=true;
                break;
            }
        }
        if(!flag)
            cout<<"NO"<<endl;
    }
    return 0;
}

PS: 使用spfa时标记最短路径,借助path数组标记,path[i]表示从s->i的最短路中经过的i的父亲节点,然后输出path[]便是从t->s的最短路径,可以借助递归输出s->t,以上题为例.

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
#define INF 0x3f3f3f3f
struct node
{
    int u;
    int v;
    ll cost;
    int next;
}edge[maxn*2];
int head[maxn],tot;
int vis[maxn],dis[maxn];
int path[maxn];   //标记源点s到i 的最短路径中,结点i的父节点
int s,t;
vector<int>vv;
void init()
{
    memset(head,-1,sizeof(head));
    memset(path,-1,sizeof(path));  //初始化
    tot=0;
    vv.clear();
}
void addedge(int u,int v,int c)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].cost=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void out()
{
    cout<<"YES"<<endl;
    for(int i=0;i<tot;i+=2)
        cout<<edge[i].u<<" "<<edge[i].v<<" "<<edge[i].cost<<endl;
}
void print_path(int point)   //输出路径
{
    if(path[point]!=-1)
        print_path(path[point]);
    cout<<point<<" ";
}
ll spfa()
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    vis[s]=1;
    queue<int>qu;
    qu.push(s);
    while(!qu.empty())
    {
        int u=qu.front();
        qu.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                path[v]=u;        //标记父亲节点
                if(!vis[v])
                {
                    vis[v]=1;
                    qu.push(v);
                }
            }
        }
    }
    return dis[t];
}
int main()
{
    int n,m,L;
    int u,v,c;
    cin>>n>>m>>L>>s>>t;
    init();
    for(int i=0;i<m;i++)
    {
        cin>>u>>v>>c;
        if(c==0)
        {
            c=INF;
            vv.push_back(tot);
        }
        addedge(u,v,c);
        addedge(v,u,c);
    }
    ll min_cost=spfa();
    if(min_cost<L)
        cout<<"NO"<<endl;
    else if(min_cost==L)
    {
        out();
        print_path(t);    //
    }
    else
    {
        bool flag=false;
        for(int i=0;i<(int)vv.size();i++)
        {
            int temp=vv[i];
            edge[temp].cost=edge[temp+1].cost=1;
            min_cost=spfa();
            if(min_cost<=L)
            {
                edge[temp].cost=edge[temp+1].cost=L-min_cost+1;
                out();
                print_path(t);  //
                flag=true;
                break;
            }
        }
        if(!flag)
            cout<<"NO"<<endl;
    }
    return 0;
}

PS:dfs优化判负环,标记点是否遍历过,只要在路径中再一次出现即为负环.

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
#define INF 0x3f3f3f3f
struct node
{
    int u;
    int v;
    ll cost;
    int next;
} edge[maxn*2];
int head[maxn],tot;
int vis[maxn],dis[maxn];
int path[maxn];   //标记源点s到i 的最短路径中,结点i的父节点
int s,t;
bool flag=false;
void init()
{
    memset(head,-1,sizeof(head));
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(path,-1,sizeof(path));  //初始化
    tot=0;
}
void addedge(int u,int v,int c)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].cost=c;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void print_path(int point)   //输出路径
{
    if(path[point]!=-1)
        print_path(path[point]);
    cout<<point<<" ";
}
void spfa(int u)
{
    vis[u]=1;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(dis[v]>dis[u]+edge[i].cost)
        {
            if(vis[v])    //负环判断
            {
                cout<<"-1"<<endl;
                flag=1;
                return ;
            }
            else
            {
                dis[v]=dis[u]+edge[i].cost;
                path[v]=u;
                spfa(v);
            }
        }
    }
    vis[u]=0;
}
int main()
{
    int n,m;
    int u,v,c;
    cin>>n>>m>>s>>t;
    init();
    for(int i=0; i<m; i++)
    {
        cin>>u>>v>>c;
        addedge(u,v,c);
        addedge(v,u,c);
    }
    dis[s]=0;
    vis[s]=1;
    spfa(s);
    if(!flag)
    {
        cout<<dis[t]<<endl;
        print_path(t);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值