求助*2

P1462 通往奥格瑞玛的道路

Mr_Greeper @ 2020-05-02 18:54:43

RT 91分代码,思路是题解第一篇,第四个测试点错误,求大佬找错,第一次求助链接here

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct edge
{
    int f,t,v,n;
}e[10000000];
struct gl
{
    int i;
    int sum;
}poi;
bool operator < (gl a,gl b)
{
    return a.sum>b.sum;
}
priority_queue < gl > q;
int head[10000000];
bool vis[10000000];
int city[10000000],s[10000000];
int dis[10000000];
int n,m,b,top,k,f,t,v,mid,l,r,ans;
bool Dijstra(int x)
{
    if(x<city[1]||x<city[n])return false;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        if(city[i]>x)vis[i]=1;
        else vis[i]=0;
    }
    poi.i=1;
    poi.sum=0;
    q.push(poi);
    for(int i=1;i<=n;i++)dis[i]=2147483647;
    dis[1]=0;
    while(!q.empty())
    {
        k=q.top().i;
        q.pop();
        if(vis[k])continue;
        vis[k]=1;
        for(int i=head[k];i;i=e[i].n)
        {
            if(dis[e[i].t]>dis[k]+e[i].v)
            {
                dis[e[i].t]=dis[k]+e[i].v;
                poi.i=e[i].t;
                poi.sum=dis[e[i].t];
                q.push(poi);
            }
        }
    }
    if(dis[n]<=b)return true;
    else return false;
}
void add_edge(int f,int t,int v)
{
    top++;
    e[top].f=f;
    e[top].t=t;
    e[top].v=v; 
    e[top].n=head[f];
    head[f]=top;
}
int main()
{
    cin>>n>>m>>b;
    for(int i=1;i<=n;i++)
    {
        cin>>city[i];
        s[i]=city[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin>>f>>t>>v;
        if(f==t)continue;
        add_edge(f,t,v);
        add_edge(t,f,v);
    }
    sort(s+1,s+1+n);
    if(!Dijstra(s[n]))
    {
        cout<<"AFK"<<endl;
        return 0;
    }
    ans=s[n];
    l=1;
    r=n;
    while(l<=r)
    {
        mid=l+r>>1;
        if(Dijstra(s[mid]))
        {
            ans=s[mid];
            r=mid-1;
        }
        else l=mid+1;
    }
    cout<<ans;
    return 0;
}

by metaphysis @ 2020-05-02 19:11:22

@Mr_Greeper

距离有可能超出int类型所能表示的范围,用long long int试试。


by Mr_Greeper @ 2020-05-02 19:24:14

@metaphysis 谢谢大佬*2


|