新手,dijkstra+二分,只有27分,求大佬帮忙看看是哪出了
查看原帖
新手,dijkstra+二分,只有27分,求大佬帮忙看看是哪出了
490264
kristinakawayi楼主2021/4/12 17:14
#include <bits/stdc++.h>

using namespace std;

#define inf 0x3f3f3f3f
struct node{
    int to;
    int blood;
};
struct point{
    int position;
    int co;
    bool operator <(const point &t) const{
        return co<t.co;
    }
};
int n,m,b;
int used[10010];
int cost[10010];
int cop[10010];//用来二分的复制数组
int dist[10010];
vector <node> G[10010];
int mi;//记录最小值
bool dj(int s){
    if(s<cop[1]||s<cop[n])return false;//初始和结束都走不了说明不行
    memset(used,0,sizeof(used));
    memset(dist,inf,sizeof(dist));
    for(int i=1;i<=n;i++){
        if(cost[i]>s){
            used[i]=1;
        }
    }
    priority_queue<point> que;
    dist[1]=0;
    point temp;
    temp.position=1;
    temp.co=0;
    que.push(temp);
    while(!que.empty()){
        temp=que.top();
        que.pop();
        int u=temp.position;
        if(used[u])continue;
        used[u]=1;
        for(int i=0;i<G[u].size();i++){
            node no=G[u][i];
            if(dist[no.to]>dist[u]+no.blood){
                dist[no.to]=dist[u]+no.blood;
                temp.position=no.to;
                temp.co=dist[no.to];
                que.push(temp);
            }
        }
    }
    if(dist[n]<b)return true;
    else return false;


}
int main(){

    cin>>n>>m>>b;
    for(int i=1;i<=n;i++){
        cin>>cost[i];
        cop[i]=cost[i];
    }
    sort(cop+1,cop+n+1);
    node temp;
    int tempx,tempy,blod;
    for(int i=0;i<n;i++){
        cin>>tempx>>tempy;
        cin>>blod;
        temp.to=tempy,temp.blood=blod;
        G[tempx].push_back(temp);
        temp.to=tempx;
        G[tempy].push_back(temp);
    }

    int left=1,right=n,mid;
    if(!dj(cop[n])){
        cout<<"AFK"<<endl;
        return 0;
    }
    mi=cop[n];
    while(left<=right){
        mid=(left+right)/2;
        if(dj(cop[mid])){
            mi=cop[mid];
            right=mid-1;
        } else left=mid+1;
    }
    cout<<mi<<endl;
    return 0;
}
2021/4/12 17:14
加载中...