最短路变短了【最短路】

题意:

题目链接
题解链接

代码:

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,int>P;
const ll inf=1e16;
const int N=1e5+5;
struct edge
{
    int u,v;
    ll w;
}e[N<<1];
vector<P>G[2][N];
priority_queue<P,vector<P>,greater<P> >que;
ll dis[2][N];
void read(int &x)
{
    x=0;
    int f=1;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    x*=f;
}
void dij(int s,int f,int n)
{
    while(!que.empty())
        que.pop();
    que.push(make_pair(0,s));
    for(int i=1;i<=n;i++)
        dis[f][i]=inf;
    dis[f][s]=0;
    while(!que.empty())
    {
        P now=que.top();
        que.pop();
        if(dis[f][now.second]<now.first)
            continue;
        for(int i=0;i<G[f][now.second].size();i++)
        {
            P tmp=G[f][now.second][i];
            if(dis[f][tmp.second]>dis[f][now.second]+tmp.first)
            {
                dis[f][tmp.second]=dis[f][now.second]+tmp.first;
                que.push(make_pair(dis[f][tmp.second],tmp.second));
            }
        }
    }
}
int main()
{
    int n,m,u,v,c,q,d;
    read(n),read(m);
    for(int i=1;i<=m;i++)
    {
        read(u),read(v),read(c);
        G[0][u].pb(make_pair(c,v));
        G[1][v].pb(make_pair(c,u));
        e[i].u=u;
        e[i].v=v;
        e[i].w=c;
    }
    dij(1,0,n);
    dij(n,1,n);
    read(q);
    while(q--)
    {
        read(d);
        if(dis[0][e[d].v]+dis[1][e[d].u]+e[d].w<dis[0][n])
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/1024-xzx/p/12682980.html