# dijkstra + 拓扑排序

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}

const int N = 40005;
int t, r, p, s, cnt, tot, head[N], c[N], d[N], dist[N];
bool vis[N];
struct Edge { int v, next, w; } edge[N<<5];

void addEdge(int a, int b, int w){
edge[cnt].v = b, edge[cnt].w = w, edge[cnt].next = head[a], head[a] = cnt ++;
}

void dfs(int s){
vis[s] = true;
c[s] = tot;
for(int i = head[s]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(vis[u]) continue;
dfs(u);
}
}

void topSort(){
full(dist, INF);
queue<int> q;
for(int i = 1; i <= tot; i ++){
if(!d[i]) q.push(i);
}
dist[s] = 0;
while(!q.empty()){
full(vis, false);
int cur = q.front(); q.pop();
priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
for(int i = 1; i <= t; i ++){
if(c[i] == cur) pq.push(make_pair(dist[i], i));
}
while(!pq.empty()){
int f = pq.top().second, dis = pq.top().first; pq.pop();
if(vis[f]) continue;
vis[f] = true;
for(int i = head[f]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(dist[u] > dis + edge[i].w){
dist[u] = dis + edge[i].w;
if(c[u] == c[f]) pq.push(make_pair(dist[u], u));
}
if(c[u] != c[f]){
if(!--d[c[u]]) q.push(c[u]);
}
}
}
}
}

int main(){

for(int i = 0; i < r; i ++){
}
for(int i = 1; i <= t; i ++){
if(!vis[i]) ++tot, dfs(i);
}
for(int i = 0; i < p; i ++){
}
for(int cur = 1; cur <= t; cur ++){
for(int i = head[cur]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(c[u] != c[cur]) d[c[u]] ++;
}
}
topSort();
for(int i = 1; i <= t; i ++){
printf(dist[i] >= 100000000 ? "NO PATH\n" : "%d\n", dist[i]);
}
return 0;
}

posted @ 2019-04-18 21:49  清楚少女ひなこ  阅读(204)  评论(0编辑  收藏  举报