标签:name mes cos 道路 产生 调查 clu jks his
Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查。他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T。这些城镇之间通过R条道路 (1 <= R <= 50,000,编号为1到R) 和P条航线 (1 <= P <= 50,000,编号为1到P) 连接。每条道路i或者航线i连接城镇A_i (1 <= A_i <= T)到B_i (1 <= B_i <= T),花费为C_i。
对于道路,0 <= C_i <= 10,000;然而航线的花费很神奇,花费C_i可能是负数(-10,000 <= C_i <= 10,000)。道路是双向的,可以从A_i到B_i,也可以从B_i到A_i,花费都是C_i。然而航线与之不同,只可以从A_i到B_i。
事实上,由于最近恐怖主义太嚣张,为了社会和谐,出台 了一些政策保证:如果有一条航线可以从A_i到B_i,那么保证不可能通过一些道路和航线从B_i回到A_i。由于FJ的奶牛世界公认十分给力,他需要运送奶牛到每一个城镇。他想找到从发送中心城镇S(1 <= S <= T) 把奶牛送到每个城镇的最便宜的方案,或者知道这是不可能的。
* Line 1: Four space separated integers: T, R, P, and S
* Lines 2..R+1: Three space separated integers describing a road: A_i, B_i and C_i
* Lines R+2..R+P+1: Three space separated integers describing a plane: A_i, B_i and C_i
6 3 3 4 1 2 5 3 4 5 5 6 10 3 5 -100 4 6 -100 1 3 -10
NO PATH NO PATH 5 0 -95 -100
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#define N 25005
#define M 200005
using namespace std;
struct Node
{
int val, pos;
friend bool operator < (Node x, Node y) {
return x.val > y.val;
}
};
struct E {int next, to, dis;} e[M];
int n, m1, m2, s, num, dfn;
int h[N], bel[N], deg[N], dis[N];
bool vis[N];
vector<int> c[N];
queue<int> que;
int read()
{
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x *= f;
}
void add(int u, int v, int w)
{
e[++num].next = h[u];
e[num].to = v;
e[num].dis = w;
h[u] = num;
}
void dfs(int x)
{
bel[x] = dfn, vis[x] = 1, c[dfn].push_back(x);
for(int i = h[x]; i != 0; i = e[i].next)
if(!vis[e[i].to]) dfs(e[i].to);
}
void dijkstra(int id)
{
priority_queue<Node> pue;
for(int i = 0; i < c[id].size(); i++) pue.push((Node){dis[c[id][i]], c[id][i]});
while(pue.size())
{
int now = pue.top().pos;
pue.pop();
if(vis[now]) continue;
vis[now] = 1;
for(int i = h[now]; i != 0; i = e[i].next)
{
if(dis[now] + e[i].dis < dis[e[i].to])
{
dis[e[i].to] = dis[now] + e[i].dis;
if(bel[now] == bel[e[i].to]) pue.push((Node){dis[e[i].to], e[i].to});
}
deg[bel[e[i].to]]--;
if(!deg[bel[e[i].to]] && bel[now] != bel[e[i].to]) que.push(bel[e[i].to]);
}
}
}
int main()
{
cin >> n >> m1 >> m2 >> s;
for(int i = 1; i <= m1; i++)
{
int u = read(), v = read(), w = read();
add(u, v, w), add(v, u, w);
}
for(int i = 1; i <= n; i++)
if(!vis[i]) ++dfn, dfs(i);
for(int i = 1; i <= m2; i++)
{
int u = read(), v = read(), w = read();
add(u, v, w);
deg[bel[v]]++;
}
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0, que.push(bel[s]);
for(int i = 1; i <= dfn; i++)
if(!deg[i]) que.push(i);
while(que.size())
{
int now = que.front();
que.pop();
dijkstra(now);
}
for(int i = 1; i <= n; i++)
if(dis[i] >= 1e9) printf("NO PATH\n");
else printf("%d\n", dis[i]);
}
标签:name mes cos 道路 产生 调查 clu jks his
原文地址:https://www.cnblogs.com/BigYellowDog/p/11373901.html