P3008 [USACO11JAN]道路和飞机Roads and Planes

题目描述

Farmer John is conducting research for a new milk contract in a new territory. He intends to distribute milk to T (1 <= T <= 25,000) towns conveniently numbered 1..T which are connected by up to R (1 <= R <= 50,000) roads conveniently numbered 1..R and/or P (1 <= P <= 50,000) airplane flights conveniently numbered 1..P.

Either road i or plane i connects town A_i (1 <= A_i <= T) to town B_i (1 <= B_i <= T) with traversal cost C_i. For roads, 0 <= C_i <= 10,000; however, due to the strange finances of the airlines, the cost for planes can be quite negative (-10,000 <= C_i <= 10,000).

Roads are bidirectional and can be traversed from A_i to B_i or B_i to A_i for the same cost; the cost of a road must be non-negative.

Planes, however, can only be used in the direction from A_i to B_i specified in the input. In fact, if there is a plane from A_i to B_i it is guaranteed that there is no way to return from B_i to A_i with any sequence of roads and planes due to recent antiterror regulation.

Farmer John is known around the world as the source of the world's finest dairy cows. He has in fact received orders for his cows from every single town. He therefore wants to find the cheapest price for delivery to each town from his distribution center in town S (1 <= S <= T) or to know that it is not possible if this is the case.

MEMORY LIMIT: 64MB

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

输出格式:

* 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.

解析:

表示用SPFA+SLF水过。。。

真的没想到能过,说明这道题的数据显然不存在重复连接两个点的道路和航线,否则用SPFA的话,还是可以走回去的。。。

数据真老。。。这样的数据的话,咱朴素的用一个SPFA多好。

加个读入优化应该能花少点时间。。。

参考代码: 1833ms /  6.86MB 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<string>
 7 #include<cstdlib>
 8 #include<queue>
 9 #include<vector>
10 #define INF 10000000
11 #define PI acos(-1.0)
12 #define N 500010
13 #define MOD 2520
14 #define E 1e-12
15 using namespace std;
16 struct rec{
17     int next,ver,edge,flag;
18 }g[N<<2];
19 deque<int> q;
20 int head[N],tot,d[N],n,r,p,s;
21 bool v[N<<2];
22 void add(int x,int y,int val)
23 {
24     g[++tot].ver=y,g[tot].edge=val;
25     g[tot].next=head[x],head[x]=tot;
26 }
27 inline void spfa(int x)
28 {
29     memset(d,0x3f,sizeof(d));
30     memset(v,0,sizeof(v));
31     d[x]=0;v[x]=1;
32     q.push_back(x);
33     while(q.size())
34     {
35         int index=q.front();q.pop_front();
36         v[index]=0;
37         for(int i=head[index];i;i=g[i].next){
38             int y=g[i].ver,z=g[i].edge;
39             if(d[y]>d[index]+z){
40                 d[y]=d[index]+z;
41                 if(!v[y]){
42                     if(!q.empty()&&d[y]>=d[q.front()]) q.push_back(y);
43                     else q.push_front(y);
44                     v[y]=1;
45                 }
46             }
47         }
48     }
49 }
50 int main()
51 {
52     scanf("%d%d%d%d",&n,&r,&p,&s);
53     tot=0;
54     for(int i=1;i<=r;i++){//road
55         int x,y,val;
56         scanf("%d%d%d",&x,&y,&val);
57         add(x,y,val),add(y,x,val);
58     }
59     for(int i=1;i<=p;i++){//plane
60         int x,y,val;
61         scanf("%d%d%d",&x,&y,&val);
62         add(x,y,val);
63     }
64     spfa(s);
65     for(int i=1;i<=n;i++){
66         if(d[i]>INF) cout<<"NO PATH"<<endl;
67         else cout<<d[i]<<endl;
68     }
69     return 0;
70 }

然而正解是topsort+dijkstra,简要思路如下(其实我没能很理解清楚):

将由双向道路连接的联通块缩点,这样原图就成了一个DAG(有向无环图),用拓扑序框架处理就能很好的求出DAG的最短路了。

(然而我没写(逃)

原文地址:https://www.cnblogs.com/DarkValkyrie/p/11025060.html