P3008 Roads and Planes G (dij + 拓扑

#include 
using namespace std;
using VI = vector;
using PII = pair;
int t,r,p,s;
VI g[250001];
VI gv[250001];
int town[250001];
VI c[250001];//记录联通块中的点
int idx = 0;
int vis[250001];
int inR[250010];
int dis[250010];
queue q;
void dfs(int x){
    c[idx].push_back(x);
    town[x] = idx;
    for(int i=0;i,greater> pq;
    for(int i=0;i dis[now] + cost){
                dis[to] = dis[now] + cost;
                if(town[to] == town[now]) pq.push({dis[to],to});
            }
            if(town[to] != town[now] && (--inR[town[to]]) == 0)q.push(town[to]);
        }
    }
}


int main(){
    cin>>t>>r>>p>>s;
    for(int i=1;i<=r;i++){
        int x,y,z;
        cin>>x>>y>>z;
        g[x].push_back(y);
        gv[x].push_back(z);
        g[y].push_back(x);
        gv[y].push_back(z);
    }
    memset(town,-1,sizeof town);
    for(int i=1;i<=t;i++){
        if(town[i] != -1) continue;
        dfs(i);
        idx++;
    }
    for(int i=1;i<=p;i++){
        int x,y,z;
        cin>>x>>y>>z;
        g[x].push_back(y);
        gv[x].push_back(z);
        inR[town[y]] ++;
    }
  
  
    memset(dis,0x3f,sizeof dis);
    dis[s] = 0;
    q.push(town[s]);
    for(int i=0;i 0x3f3f3f3f/2)cout<<"NO PATH"<<"\n";
        else cout<

你可能感兴趣的:(图,深度优先,图论,算法)