二分+SPFA

10 篇文章 0 订阅
2 篇文章 0 订阅

洛谷 P1951 收费站
题目分析:题目中是要求从s到t的路径上,经过的收费站所收取的费用的最大值最小。所以,我们可以想到二分答案!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<deque>
#define fp(i,a,b) for(register int i=a;i<=b;++i)
using namespace std;
struct arr{
    int nd,nx,co;
}bot[100100];
int n,m,b,cnt,s,t;
long long head[20000],dist[20000],vis[20000],w[20000],cost[20000];
inline int read(){
    int x=0,w=1;char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') w=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar();
    return x*w;
}
inline int cmp(long long a,long long b){return a<b;}
inline void add(int a,int b,int c){bot[++cnt].nd=b;bot[cnt].nx=head[a];bot[cnt].co=c;head[a]=cnt;}
inline int SPFA(int x){
    if(x<w[s]) return 0;//原点也要包含在这里面
    fp(i,1,n) dist[i]=1147483647,vis[i]=0;
    deque<int>q;
    dist[s]=0;q.push_back(s);vis[s]=1;
    while(!q.empty()){
        int u=q.front(),v;q.pop_front();
        vis[u]=0;
        for(register int i=head[u];i;i=bot[i].nx){
            int v=bot[i].nd;
            if(w[v]<=x&&dist[v]>dist[u]+bot[i].co){
    //除了普通的SPFA的判断,还要加一个当前点的收取费用是否是小于等于二分的x值
                dist[v]=dist[u]+bot[i].co;
                if(!vis[v]){
                    vis[v]=1;
                    if(q.empty()||dist[v]>dist[q.front()])    
                         q.push_back(v);
                    else q.push_front(v);
                }
            }
        }
    }
    return dist[t]<b; //返回值
}
int main(){
    n=read();m=read();s=read();t=read();b=read();
    fp(i,1,n) w[i]=cost[i]=read();
    fp(i,1,m){int u=read(),v=read(),ww=read();add(u,v,ww);add(v,u,ww); } 
    //双向边
    sort(1+cost,1+n+cost,cmp);
    int l=1,r=n, mid,ans=-1;
    while(l<=r){//二分答案部分
        mid=(l+r)>>1;
        if(SPFA(cost[mid])) ans=cost[mid],r=mid-1;
        else l=mid+1;
    }
    cout<<ans<<endl;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java代码实现: ```java import java.io.*; import java.util.Arrays; public class Main { static int N = 10010, M = N * 2, idx = 1; static int[] h = new int[N], e = new int[M], w = new int[M], ne = new int[M]; static int n, m, res = 0; static int[] dist = new int[N]; static boolean[] st = new boolean[N]; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line = br.readLine().split(" "); n = Integer.parseInt(line[0]); m = Integer.parseInt(line[1]); Arrays.fill(h, -1); while (m-- > 0) { line = br.readLine().split(" "); int a = Integer.parseInt(line[0]), b = Integer.parseInt(line[1]), c = Integer.parseInt(line[2]); char d = line[3].charAt(0); int distance = getDistance(a, b, c, d); add(a, b, distance); add(b, a, distance); } spfa(); for (int i = 1; i <= n; i++) res = Math.max(res, dist[i]); System.out.println(res); } private static void spfa() { Arrays.fill(dist, -0x3f3f3f); dist[1] = 0; for (int i = h[1]; i != -1; i = ne[i]) dist[e[i]] = w[i]; st[1] = true; for (int i = 0; i < n - 1; i++) { int t = -1; for (int j = 1; j <= n; j++) if (!st[j] && (t == -1 || dist[t] < dist[j])) t = j; st[t] = true; for (int j = h[t]; j != -1; j = ne[j]) { int ver = e[j], distance = w[j]; if (dist[ver] < dist[t] + distance) { dist[ver] = dist[t] + distance; } } } } private static int getDistance(int a, int b, int c, char d) { if (d == 'N') return c; else if (d == 'S') return c; else if (d == 'W') return c; else return c; } private static void add(int a, int b, int c) { e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx++; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值