icpc 银川 H. Delivery Route SFPA优化

Problem Description

Pony is the boss of a courier company. The company needs to deliver packages to n offices numbered from 1 to n. Especially, the s-th office is the transfer station of the courier company. There are x ordinary two-way roads and y one-way roads between these offices. The delivery vans will consume ci power if they pass through the i-th road. In general, the power consumption on one road must be non-negative. However, thanks to the experimental charging rail, the consumption may be negative on some one-way roads. Besides, Pony got the following public information. The relevant department promised that if there is a one-way street from ai to bi, it is impossible to return from bi to ai. To avoid the delivery vans anchoring on the road, Xiaodao wants to find these lowest power consumptions from the transfer station to these offices

Input

The first line contains four integers n (1 ≤ n ≤ 25000); x; y (1 ≤ x; y ≤ 50000), and s (1 ≤ s ≤ n). This is followed by x + y lines, each line of which contains three integer ai; bi (1 ≤ ai; bi ≤ n; ai 6= bi) and ci (−10000 ≤ ci ≤ 10000) describing the roads. The first x given roads are ordinary two-way roads, and the last y given roads are one-way roads.

Output

The output should contain n lines, the i-th line represents the minimum energy consumption from s-th to the i-th office if possible, or output “NO PATH” if it is impossible to reach the i-th office.

Sample Input

6 3 3 4
1 2 5
3 4 5
5 6 10
3 5 -100
4 6 -100
1 3 -10

Sample Output

NO PATH
NO PATH
50
-95
-100

Analysis of ideas

Accepted code


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int N=1e6;
int p[N],w[N],head[N],nex[N],e=1;
void add(int a,int b,int c){
    p[e]=b;
    nex[e]=head[a];
    head[a]=e;
    w[e++]=c;
}
 
long long dp[100010],val=0;
int q[20000010],l,r,vis[100010];
void bfs(int s,int n){
     int i,u,v;
     for(i=1;i<=n;i++) dp[i]=1e18;
     dp[s]=0;
     l=r=1e7;
     q[l]=s;
     vis[s]=1;
     while(l<=r){
        u=q[l];
        vis[u]=0;
        l++;
        for(i=head[u];i;i=nex[i]){
            v=p[i];
            if(dp[v]>dp[u]+w[i]){
                dp[v]=dp[u]+w[i];
                if(vis[v]==0){
                    if(l>r) q[++r]=v;
                    else{
                        if(dp[q[l]]+val<dp[v]) q[++r]=v;
                        else q[--l]=v;
                    }
                    vis[v]=1;
                }
            }
        }
     }
     for(i=1;i<=n;i++){
        if(dp[i]==1e18) printf("NO PATH\n");
        else printf("%lld\n",dp[i]);
     }
}
 
int main(){
    int i,n,x,y,s,a,b,c;
    scanf("%d%d%d%d",&n,&x,&y,&s);
    for(i=1;i<=x;i++){
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
        val+=2*c;
    }
    for(i=1;i<=y;i++){
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        val+=c;
    }
    val=sqrt(val)/100;
    bfs(s,n);
    return 0;
}

参考博客:

猜你喜欢

转载自www.cnblogs.com/hezongdnf/p/11967069.html
今日推荐