POJ3114 Countries in War - Tarjan缩点+堆优化Dijkstra

题目连接

http://poj.org/problem?id=3119

Description

In the year 2050, after different attempts of the UN to maintain peace in the world, the third world war broke out. The importance of industrial, commercial and military secrets obliges all the countries to use extremely sophisticated espionage services, so that each city in the world has at least one spy of each country. These spies need to communicate with other spies, informers as well as their headquarters during their actions. Unluckily there doesn’t exist a secure way for a spy to communicate during the war period, therefore the messages are always sent in code so that only the addressee is able to read the message and understand its meaning.

The spies use the only service that functions during the war period, the post. Each city has a postal agency where the letters are sent. The letters can be sent directly to their destination or to other postal agencies, until the letter arrives at the postal agency of the destination city, if possible.

The postal agency in city A can send a printed letter to the postal agency in city B if there is an agreement on sending letters, which determines the time, in hours, that a letter takes to reach city B from city A (and not necessarily the opposite). If there is no agreement between the agencies A and B, the agency A can try to send the letter to any agency so that the letter can reach its destination as early as possible

Some agencies are connected with electronic communication media, such as satellites and optical fibers. Before the war, these connections could reach all the agencies, making that a letter could be sent instantly. But during the period of hostilities every country starts to control electronic communication and an agency can only send a letter to another agency by electronic media (or instantly) if they are in the same country. Two agencies, A and B, are in the same country if a printed letter sent from any one of the agencies can be delivered to the other one.

The espionage service of your country has managed to obtain the content of all the agreements on sending messages existing in the world and desires to find out the minimum time to send a letter between different pairs of cities. Are you capable of helping them?

Input

The input contains several test cases. The first line of each test case contains two integer separated by a space, N (1 ≤ N ≤ 500) and E (0 ≤ E ≤ N2), indicating the numbers of cities (numbered from 1 to N) and of agreements on sending messages, respectively. Following them, then, E lines, each containing three integers separated by spaces, X, Y and H (1 ≤ X, Y ≤ N, 1 ≤ H ≤ 1000), indicating that there exist an agreement to send a printed letter from city X to city Y, and that such a letter will be delivered in H hours.

After that, there will be a line with an integer K (0 ≤ K ≤ 100), the number of queries. Finally, there will be K lines, each representing a query and containing two integers separated by a space, O and D (1 ≤ O, D ≤ N). You must determine the minimum time to send a letter from city O to city D.

The end of the input is indicated by N = 0.

Output

For each test case your program should produce K lines of output. The I-th line should contain an integer M, the minimum time, in hours, to send a letter in the I-th query. If there aren’t communication media between the cities of the query, you should print “Nao e possivel entregar a carta” (“It’s impossible to deliver the letter”).

Print a blank line after each test case.

Sample Input

4 5
1 2 5
2 1 10
3 4 8
4 3 7
2 3 6
5
1 2
1 3
1 4
4 3
4 1
3 3
1 2 10
2 3 1
3 2 1
3
1 3
3 1
3 2
0 0

Sample Output

0
6
6
0
Nao e possivel entregar a carta

10
Nao e possivel entregar a carta
0

题意
第三次世界大战爆发,现在每个城市的间谍都通过邮局进行通信交流,A、B两个点发送邮件能立刻完成当且仅当其在同一个城市中,即两个点在同一个城市当中。询问任意两点间发送邮件所需要的最短时间。

题解

缩点成为DAG图之后跑Dijkstra即可,白书上的模板用了很多次但是有几次包括这次WA掉了果断换模板AC之。原本以为跑Floyd能过,但要查询的重复点似乎不多,Floyd反而爆掉了,这一点比较坑。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
const int maxn = 500+10;
const int inf = INT_MAX;
int n,m,id,head[maxn],flag[maxn],tot,tag,dfn[maxn],low[maxn],vis[maxn],gone[maxn];
int iid,hhead[maxn];
stack<int> S;
struct Edge{
int from,to,next,h;
}E[maxn*maxn],e[maxn*maxn];
struct Node{
int dis,id;
friend bool operator < (Node A,Node B){
return A.dis > B.dis;
}
}ans[maxn];
priority_queue<Node> que;
void init(){
id=tag=tot=iid=0;
memset(head,-1,sizeof head);
memset(hhead,-1,sizeof hhead);
memset(dfn,0,sizeof dfn);
memset(low,0,sizeof low);
}
void addEdge(int u,int v,int h){
E[id].from = u;
E[id].to = v;
E[id].next = head[u];
E[id].h = h;
head[u]=id++;
}
void addEdge2(int u,int v,int h){
e[iid].from = u;
e[iid].to = v;
e[iid].next = hhead[u];
e[iid].h = h;
hhead[u]=iid++;
}
void tarjan(int x){
dfn[x]=low[x]=++tot;
S.push(x);
vis[x]=1;
for(int i=head[x];i!=-1;i=E[i].next){
int v=E[i].to;
if(!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
}
else if(vis[v])
low[x]=min(low[x],dfn[v]);
}
if(low[x]==dfn[x]){
++tag;
while(1){
int now=S.top();
S.pop();
vis[now]=0;
flag[now]=tag;
if(now==x) break;
}
}
}
//书本上没有加gone标记的模板会WA,故换此模板。
void Dijkstra(int x){
while(!que.empty()) que.pop();
//记得清空访问标记。
memset(gone,0,sizeof gone);
for(int i=1;i<=tag;++i){
ans[i].dis=inf;
ans[i].id=i;
}
ans[x].dis = 0;
que.push(ans[x]);
while(!que.empty()){
Node u = que.top();
que.pop();
//若当前点u松弛过了直接continue,不加这个会WA,同时未访问就标记。
if(gone[u.id]) continue;
gone[u.id] = 1;
for(int i=hhead[u.id];i!=-1;i=e[i].next){
int v=e[i].to;
//这里的工作就是通过u可以用的边松弛它到其他各个点的距离。
ans[v].dis = min(ans[v].dis,u.dis+e[i].h);
//松弛完之后加入队列。
que.push(ans[v]);
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
if(n==0 && m==0) break;
init();
int u,v,h;
for(int i=0;i<m;++i){
scanf("%d%d%d",&u,&v,&h);
addEdge(u,v,h);
}
for(int i=1;i<=n;++i)
if(!dfn[i])
tarjan(i);
for(int i=0;i<id;++i){
u=flag[E[i].from],v=flag[E[i].to];
if(u!=v){
addEdge2(u,v,E[i].h);
}
}
int k;
scanf("%d",&k);
int o,d;
for(int i=0;i<k;++i){
scanf("%d%d",&o,&d);
if(flag[o]==flag[d])
printf("0\n");
else{
Dijkstra(flag[o]);
if(ans[flag[d]].dis!=inf)
printf("%d\n",ans[flag[d]].dis);
else
printf("Nao e possivel entregar a carta\n");
}
}
printf("\n");
}
}