【洛谷 1396】营救

题目描述

“咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门……

妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了t区,而自己在s区。

该市有m条大道连接n个区,一条大道将两个区相连接,每个大道有一个拥挤度。小明的妈妈虽然很着急,但是不愿意拥挤的人潮冲乱了她优雅的步伐。所以请你帮她规划一条从s至t的路线,使得经过道路的拥挤度最大值最小。

输入格式

第一行四个数字n,m,s,t。

接下来m行,每行三个数字,分别表示两个区和拥挤度。

(有可能两个区之间有多条大道相连。)

输出格式

输出题目要求的拥挤度。

输入输出样例

输入 #1
3 3 1 3							
1 2 2
2 3 1
1 3 3
输出 #1
2

说明/提示

数据范围

30% n<=10

60% n<=100

100% n<=10000,m<=2n,拥挤度<=10000

题目保证1<=s,t<=n且s<>t,保证可以从s区出发到t区。

样例解释:

小明的妈妈要从1号点去3号点,最优路线为1->2->3。

题解:最短路,用SPFA+二分答案即可过,

唉,0和oo没看清,导致找bug找半天,失望!

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue> 
typedef long long ll;
using namespace std;
const int N=40005;
const int oo=0x3f3f3f3f;
queue<int>q;
int n,m,s,t,z,cnt,x,y,dis[N],vis[N];
struct node{
    int to;
    int val;
    int next;
}e[N];
int head[N];
void add(int a,int b,int c){
    cnt++;
    e[cnt].to=b;
    e[cnt].val=c;
    e[cnt].next=head[a];
    head[a]=cnt;
}
bool Yao_Chen_Lai_Le(int biu){
    memset(dis,0x3f,sizeof(dis));
    q.push(s);
    dis[s]=1;
    //vis[s]=1;
    while(!q.empty()){
        x=q.front();
        q.pop();
        //vis[x]=0;
        for(int i=head[x];i!=0;i=e[i].next){
            int too=e[i].to;
            /*if(dis[too]>dis[x]+e[i].val)
            { 
               dis[too]=dis[x]+e[i].val;
                if(vis[too]==0){
                    vis[too]=1; q.push(too);
                }
            } */
            if(e[i].val<=biu){
                if(dis[too]==oo){
                    dis[too]=1; q.push(too);
                }
            } 
        }
    }
    if(dis[t]==oo) return 0;
    else return 1;
}

void jjj_ef(){
    int l=0;
    int r=oo;
    while(l<r){
        int mid=(l+r)/2;
        if (Yao_Chen_Lai_Le(mid)) r=mid;
        else l=mid+1;
    }
    printf("%d",l);
} 
int main(){
    freopen("1396.in","r",stdin);
    freopen("1396.out","w",stdout);
    scanf("%d %d %d %d",&n,&m,&s,&t);
    for(int i=1;i<=m;i++){
        scanf("%d %d %d",&x,&y,&z);
        add(x,y,z); add(y,x,z);
    }
    jjj_ef();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wuhu-JJJ/p/11273452.html