[Acwing] 342. 道路与航线

前言

t a g : tag : tag: 拓扑 堆优化DIJ 思维
传送门 :

题意 :

给定多个双向边和多个单向边,双向边边权必然为正,单向边可能为负,每个双向边构成的连通块只能由单向边相连,询问从 S S S开始到其他点的最短路

思路 :

首先题意很明显的想让我们分块处理,即先处理出双向边的所有连通块,这样子整个图,就会变成一个 D A G DAG DAG

下面这里就有一条需要注意的是 :
D A G DAG DAG上 我们可以线性的时间求出单源最短路

因此我们直接对 D A G DAG DAG求一个最短路即可

code :

// Problem: 道路与航线
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/344/
// Memory Limit: 64 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// Problem: B. Rising Sand
// Contest: Codeforces - Codeforces Round #803 (Div. 2)
// URL: https://codeforces.com/contest/1698/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <queue>
#include <math.h>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define IOS  ios::sync_with_stdio(false);
#define CIT  cin.tie(0);
#define COT  cout.tie(0);

#define ll long long
#define x first
#define y second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(),x.end()
typedef pair<int,int> pii;
#define Fup(i,a,b) for(int i=a;i<=b;i++)
#define Fde(i,a,b) for(int i=a;i>=b;i--)
#define cer(a) cerr<<#a<<'='<<(a)<<" @ line "<<__LINE__<<" "<<endl
typedef priority_queue<pii,vector<pii>,greater<pii>>  Pri_m;
typedef vector<int> VI;
map<int,int> mp;
const int N = 2e5+10,INF = 0x3f3f3f3f;
const double eps = 1e-5;


struct node{
    int to,val;
};


int t,r,p,s;
int id[N],bcnt;
int deg[N];

vector<int> block[N];
int i,j;

vector<node> road[N];

int dist[N];
int st[N];
	queue<int> q;
void dfs(int u){
	block[bcnt].pb(u);
	id[u] = bcnt;
	
	for(auto x : road[u]){
		int j = x.to;
		if(!id[j])dfs(j);
	}
}

void dij(int block_id){
	Pri_m heap;
	for(auto x : block[block_id]) heap.push({dist[x],x});
	
	while(heap.size()){
		auto t = heap.top();
		heap.pop();
		
		int u = t.y;
		if(st[u]) continue;
		st[u] = 1;
		
		for(auto x : road[u]){
			int j = x.to;
			if(dist[j] > dist[u] + x.val){
				dist[j] = dist[u] + x.val;
				if(id[j] == block_id)heap.push({dist[j],j});	
			}
			if(id[j]!=block_id && --deg[id[j]] == 0){
				q.push(id[j]);
			}
		}
	}
}
void topsort(){
	memset(dist,0x3f,sizeof dist);
	dist[s] = 0 ;
	
	//找的是连通块
	Fup(i,1,bcnt){
		if(!deg[i])q.push(i);
	}
	
	while(!q.empty()){
		auto  t = q.front();
		q.pop();
		dij(t);
	}
}
void solve(){
	cin>>t>>r>>p>>s;
	
	Fup(i,1,r){
		int a,b,c;cin>>a>>b>>c;
		road[a].pb({b,c});
		road[b].pb({a,c});
	}
	
	Fup(i,1,t){
		if(!id[i]){
			++bcnt;
			dfs(i);
		}
	}
	
	Fup(i,1,p){
		int a,b,c;cin>>a>>b>>c;
		road[a].pb({b,c});
		deg[id[b]]++;
	}
	
	topsort();
	
	
	Fup(i,1,t){
		if(dist[i] > INF/2)cout<<"NO PATH"<<endl;
		else cout<<dist[i]<<endl;
		
	}
	
}

int main(){
	//int t;cin>>t;while(t--)
    solve();
    return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值