题目描述
Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查。他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1~T。这些城镇之间通过R条道路 (1 <= R <= 50,000,编号为1到R) 和P条航线 (1 <= P <= 50,000,编号为1到P) 连接。每条道路i或者航线i连接城镇 (1 <= <= T)到 (1 <= <= T),花费为。
对于道路,0 <= <= 10,000;然而航线的花费很神奇,花费可能是负数(-10,000 <= <= 10,000)。道路是双向的,可以从到,也可以从到,花费都是。然而航线与之不同,只可以从到。
事实上,由于最近恐怖主义太嚣张,为了社会和谐,出台 了一些政策保证:如果有一条航线可以从到,那么保证不可能通过一些道路和航线从回到。由于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
输出格式:
* Lines 1..T: The minimum cost to get from town S to town i, or 'NO PATH' if this is not possible
输入输出样例
输入样例#1:
6 3 3 4
1 2 5
3 4 5
5 6 10
3 5 -100
4 6 -100
1 3 -10
输出样例#1:
NO PATH
NO PATH
5
0
-95
-100
说明
6 towns. There are roads between town 1 and town 2, town 3 and town 4, and town 5 and town 6 with costs 5, 5 and 10; there are planes from town 3 to town 5, from town 4 to town 6, and from town 1 to town 3 with costs -100, - 100 and -10. FJ is based in town 4.
FJ's cows begin at town 4, and can get to town 3 on the road. They can get to towns 5 and 6 using planes from towns 3 and 4. However, there is no way to get to towns 1 and 2, since they cannot go
backwards on the plane from 1 to 3.
题目地址:https://www.luogu.org/problemnew/show/P3008
个人思路:
- 首先可以发现是单源最短路问题,但是负边权引起了我们的注意。
- "双向道路单向航线"、"不存在环"引起了我们的注意,可以发现缩点后是一个DAG图。
- 在DAG图上可以考虑进行拓扑排序。同时在每个联通块内部进行Dijkstra算法,问题得解。
数据注意点:
- 拖油瓶式数据
- 拓扑排序(INF)式数据
-
3 0 2 1 1 2 1 3 2 2 简单 拖油瓶式数据 9 9 2 1 1 2 1 1 3 3 2 3 7 4 5 2 5 6 1 4 6 6 7 8 1 8 9 1 9 7 1 3 4 5 7 6 5 普通 拖油瓶式数据 3 0 1 1 3 2 -100 简单 拓扑排序(INF)式数据
测试数据(仅供参考):https://gitee.com/fanlab/codes/i5xqrg21smv0be49djn8389
#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int MAXN=1000010,MAXM=1000010,INF=2100000000;
struct Edge_Default{
int from,to,w,nxt;
}e[MAXM];
int head[MAXN],edgeCnt=0;
void addEdge(int u,int v,int w){
e[++edgeCnt].from=u;
e[edgeCnt].to=v;
e[edgeCnt].w=w;
e[edgeCnt].nxt=head[u];
head[u]=edgeCnt;
}
vector<int> scc[MAXN];//联通块
int c[MAXN];//属于的联通块的代表元
void getScc(int fa,int x){//DFS划分联通块
c[x]=fa;
scc[c[fa]].push_back(x);
for(int i=head[x];i;i=e[i].nxt){
int nowV=e[i].to;
if(!c[nowV]){
getScc(fa,nowV);
}
}
}
int rd[MAXN];
//从某个点开始进行dijkstra,出现不在联通块的则加入bfs队列,并减少入度
struct Node{
int nowPoint,nowValue;
bool operator <(const Node &a)const{
return a.nowValue<nowValue;
}
};
queue<int> topoQueue;
int dis[MAXN];
void dijkstra(int x){
priority_queue<Node> q;
if(!scc[c[x]].empty()){
int siz=scc[c[x]].size();
for(int j=0;j<siz;j++){
int nowSccPoint=scc[c[x]].at(j);
q.push(Node{nowSccPoint,dis[nowSccPoint]});
}
}
while(!q.empty()){
Node nowNode=q.top();q.pop();
int nowPoint=nowNode.nowPoint,nowValue=nowNode.nowValue;
if(dis[nowPoint]!=nowValue)continue;
for(int i=head[nowPoint];i;i=e[i].nxt){
int toV=e[i].to,fromV=e[i].from;
bool isSameScc=(c[x]==c[toV]);
if(!isSameScc){
rd[c[toV]]--;
if(!rd[c[toV]]){
topoQueue.push(c[toV]);
}
}
if(dis[fromV]==INF)continue;//*防止拓扑排序(INF)类数据
if(dis[toV]>dis[fromV]+e[i].w){
dis[toV]=dis[fromV]+e[i].w;
if(isSameScc)q.push(Node{toV,dis[toV]});
}
}
}
}
int s,vis[MAXN];
int n;
void solve(){
memset(vis,0,sizeof(vis));
dis[s]=0;
for(int i=1;i<=n;i++){
if(!vis[c[i]]&&(c[i])&&(!rd[c[i]])){
vis[c[i]]=1;
topoQueue.push(c[i]);//*防止拖油瓶式数据
}
}
while(!topoQueue.empty()){
int nowV=topoQueue.front();topoQueue.pop();//当前所在的联通块
dijkstra(nowV);//在当前联通块进行dijkstra
}
}
int main(){
memset(c,0,sizeof(c));
memset(rd,0,sizeof(rd));
int m,p;//m 双向 p单向存负
scanf("%d%d%d%d",&n,&m,&p,&s);
for(int i=1;i<=n;i++)dis[i]=INF;
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
addEdge(v,u,w);
}
for(int i=1;i<=n;i++)
if(!c[i])
getScc(i,i);
for(int i=1;i<=p;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w);
rd[c[v]]++;
}
solve();
for(int i=1;i<=n;i++){
if(dis[i]==INF)cout<<"NO PATH"<<endl;
else cout<<dis[i]<<endl;
}
return 0;
}