2019 ICPC Asia Yinchuan Regional-H. Delivery Route(dijkstra+拓扑排序)
题面:
题意:
给定一个含有\(\mathit n\) 个点,\(\mathit x\)个双向边,\(\mathit y\)个单向边的图,其中双向边的权值一定为正整数,单向边的权值有可能为负整数。并且保证图中若有一个\(u->v\)的单向路径,则一定不存在\(v->u\)的路径。
求以\(\mathit s\)为源点到各个点的最短路径值,若无法到达某个点,则输出no path。
思路:
根据图的保证可知:
将双向边构成的连通块缩成点时,整个图变成了一个有向无环图,连通块内不会存在有向边。
我们先将无向边构成的图用dfs进行连通块染色(缩点),然后根据单向边建立缩点后的有向无环图(DAG),
从DAG中入度为0的点加入队列开始拓扑排序,对于每一个连通块,我们将块中的点放入优先队列后用dijkstra算法跑出最短路,对于这些点中连出的单向边,我们更新答案但是不放入优先队列,同时将指向的连通块入度减一,以此就可以高效正确的单源最短路。
注意本题有一个坑点:
连通块的点\(now.to\)连出的单向边,要当且仅当:\(dis[now.to] != inf\) 才要去更新,
不然可能会本该no path的点,输出了数值。
代码:
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 25000 + 10;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node {
int to;
int val;
node() {}
node(int xx, int yy)
{
to = xx; val = yy;
}
bool operator < (const node &b)const
{
return val > b.val;
}
};
priority_queue<node> dij_q;
std::vector<node> e[maxn];
int n, m1, m2, s;
int col[maxn], in[maxn], dis[maxn];
int vis[maxn];
vector<int> v[maxn];
int id = 0;
void dfs(int x)
{
col[x] = id;
for (auto y : e[x]) {
if (!col[y.to]) {
dfs(y.to);
}
}
}
void dij()
{
queue<int> q;
repd(i, 1, id) {
if (!in[i]) {
q.push(i);
}
}
while (!q.empty()) {
int temp = q.front();
q.pop();
for (auto x : v[temp]) {
dij_q.push(node(x, dis[x]));
}
node now;
while (!dij_q.empty()) {
now = dij_q.top();
dij_q.pop();
if (vis[now.to]) { continue; }
vis[now.to] = 1;
for (auto x : e[now.to]) {
if (col[x.to] == col[now.to]) {
if (dis[x.to] > dis[now.to] + x.val) {
dis[x.to] = dis[now.to] + x.val;
dij_q.push(node(x.to, dis[x.to]));
}
}
if (col[x.to] != col[now.to]) {
if (dis[now.to] != inf && dis[x.to] > dis[now.to] + x.val) {
dis[x.to] = dis[now.to] + x.val;
}
--in[col[x.to]];
if (!in[col[x.to]]) {
q.push(col[x.to]);
}
}
}
}
}
}
int main()
{
freopen("D:\\code\\input.txt", "r", stdin);
//freopen("D:\\code\\output.txt","w",stdout);
n = readint();
m1 = readint(); m2 = readint(); s = readint();
repd(i, 1, m1) {
int x = readint(); int y = readint();
int w = readint();
e[x].pb(node(y, w));
e[y].pb(node(x, w));
}
repd(i, 1, n) {
if (!col[i]) {
id++;
dfs(i);
}
dis[i] = inf;
}
dis[s] = 0;
repd(i, 1, n) {
v[col[i]].pb(i);
}
repd(i, 1, m2) {
int x = readint(); int y = readint();
int w = readint();
e[x].pb(node(y, w));
in[col[y]]++;
}
dij();
repd(i, 1, n) {
if (dis[i] == inf) {
printf("NO PATH\n");
} else {
printf("%d\n", dis[i]);
}
}
return 0;
}