堆优化dij TLE?

P1462 通往奥格瑞玛的道路

Frozencode @ 2019-07-02 15:48:05

难道只是因为我用了vector存边??? 不开O2 64 开O2就过了...迷

#include<bits/stdc++.h>
using namespace std;
#define P pair<ll,ll>
typedef long long ll;
const ll maxn=10010;
const ll INF=2147483647;
struct node
{
    ll to,val;
};
node cur;
P icur; 
priority_queue<P,vector<P>,greater<P> >q;
ll n,m,k,c[maxn],dis[maxn],x,y,p,l,r;
vector<node> e[maxn];
bool vis[maxn];
bool dijkstra(ll x)
{
    dis[1] = 0;
    q.push(make_pair(0,1));
    while(!q.empty())
    {
        icur = q.top();
        q.pop();
        if(!vis[icur.second])
        {
            vis[icur.second] = 1;
            for(int i = 0;i < e[icur.second].size();i++)
            {
                cur = e[icur.second][i];
                if(c[cur.to] > x)continue;
                dis[cur.to] = min(dis[cur.to],dis[icur.second] + cur.val);
                q.push(make_pair(dis[cur.to],cur.to));
            }
        }
    }
    if(dis[n] <= k)return true;
    return false;
}
int main()
{
    cin>>n>>m>>k;
    for(int i = 1;i <= n;i++)
    {
        cin>>c[i];
        r = max(r,c[i]);
    }
    l = max(c[1],c[n]) - 1;
    while(m--)
    {
        cin>>x>>y>>p;
        cur.val = p;
        cur.to = x;
        e[y].push_back(cur);
        cur.to = y;
        e[x].push_back(cur);
    }
    for(int i = 1;i <= n;i++)dis[i] = INF;
    if(!dijkstra(INF))
    {
        cout<<"AFK";
        return 0;
    }
    while(l < r - 1)
    {
        for(int i = 1;i <= n;i++)dis[i] = INF;
        memset(vis,0,sizeof(vis));
        ll mid = (l + r)>>1;
        if(dijkstra(mid))r = mid;
        else l = mid;
    }
    cout<<r;
    return 0;
}

by Frozencode @ 2019-07-02 15:48:34

今天洛谷最短路的讨论被我承包了(捂脸


by Erusel @ 2019-07-02 16:01:03

可能吧,你用了一大堆STL


by garbage2 @ 2019-07-02 16:15:21

@Robinzh 今天线段树的讨论被我包了


by TopCarry @ 2019-07-02 16:25:25

@Frozencode 您的dijkstra写的有问题啊。。。

dis[cur.to] = min(dis[cur.to],dis[icur.second] + cur.val);
                q.push(make_pair(dis[cur.to],cur.to));

无论是否成功都会往里扔一个

if(dis[cur.to]>dis[icur.second] + cur.val){
                    dis[cur.to] =dis[icur.second] + cur.val
                    q.push(make_pair(dis[cur.to],cur.to));
                }

by Frozencode @ 2019-07-02 17:01:02

@TopCarry 所以应该是成功才扔吗qwq


by Frozencode @ 2019-07-02 17:04:49

@TopCarry 感谢聚聚,AC了


by TopCarry @ 2019-07-02 18:56:36

@Frozencode 不然你每次跑的都是满的,虽然复杂度没变。


by 龙·海流 @ 2019-08-02 11:01:21

可能有负权边


|