Countries in War
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Appoint description:
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
题意:间谍在战争时期想要传递一份邮件回国,邮件可以在各个邮局之间传播,但传递是单向的,并且耗时,如果两个邮局在一个国家的话,那么邮件在他们之间的传递不用耗时,判断两个邮局是否在一个国家的标准是两个邮局可以互相传递邮件。给出两个邮局,输出最短的到达时间。
思路:强连通分量+缩点+拓扑排序+DAG最短路。
<span style="font-size:18px;">#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <queue> #include <stack> using namespace std; const double PI = acos(-1.0); const double e = 2.718281828459; const double eps = 1e-8; const int MAXN = 510; const int INF = 1<<30; struct Edge { int v; int w; int next; } edge[MAXN*MAXN], edge1[MAXN*MAXN]; struct shrink_point { int out; int in; int num; } sp[MAXN]; int g[MAXN][MAXN]; int LOW[MAXN]; int DFN[MAXN]; int instack[MAXN]; int scc[MAXN]; int head[MAXN*MAXN]; int head1[MAXN*MAXN]; int indegree[MAXN]; int que[MAXN]; int dis[MAXN]; stack<int>Q; int n, m, t, iq; int edge_cnt; int edge_cnt1; int scc_cnt; int dfs_cnt; void add_edge(int u, int v, int w) { edge[edge_cnt].v = v; edge[edge_cnt].w = w; edge[edge_cnt].next = head[u]; head[u] = edge_cnt++; } void add_edge1(int u, int v, int w) { edge1[edge_cnt1].v = v; edge1[edge_cnt1].w = w; edge1[edge_cnt1].next = head1[u]; head1[u] = edge_cnt1++; } void Tarjan(int u) { DFN[u] = LOW[u] = ++dfs_cnt; instack[u] = 1; Q.push(u); for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if(!DFN[v]) { Tarjan(v); if(LOW[u] > LOW[v]) LOW[u] = LOW[v]; } else if(instack[v] && LOW[u]>DFN[v]) LOW[u] = DFN[v]; } if(LOW[u] == DFN[u]) { scc_cnt++; int j; do { j = Q.top(); Q.pop(); instack[j] = 0; scc[j] = scc_cnt; sp[scc_cnt].num++; } while(j != u); } return ; } void Toposort() { iq = 0; memset(que, 0, sizeof(que)); for(int i = 1; i <= scc_cnt; i++) { if(indegree[i] == 0) que[iq++] = i; } for(int i = 0; i < iq; i++) { for(int k = head1[que[i]]; k != -1; k = edge1[k].next) { int v = edge1[k].v; indegree[v]--; if(indegree[v] == 0) que[iq++] = v; } } } void solve() { memset(LOW, 0, sizeof(LOW)); memset(DFN, 0, sizeof(DFN)); memset(instack, 0, sizeof(instack)); memset(scc, 0, sizeof(scc)); memset(sp, 0, sizeof(sp)); scc_cnt = dfs_cnt = 0; while(!Q.empty()) Q.pop(); for(int i = 1; i <= n; i++) if(!DFN[i]) Tarjan(i); for(int i = 1; i <= scc_cnt; i++) for(int j = 1; j <= scc_cnt; j++) g[i][j] = (i==j)?0:INF; int u, v, w; edge_cnt1 = 0; memset(edge1, 0, sizeof(edge1)); memset(head1, -1, sizeof(head1)); memset(indegree, 0, sizeof(indegree)); for(int i = 1; i <= n; i++) { for(int k = head[i]; k != -1; k = edge[k].next) { int j = edge[k].v; if(scc[i] != scc[j]) { u = scc[i]; v = scc[j]; w = edge[k].w; if(g[u][v] > w) g[u][v] = w; } } } for(int i = 1; i <= scc_cnt; i++) { for(int j = 1; j <= scc_cnt; j++) { if(g[i][j]!=INF && i!=j) { add_edge1(i, j, g[i][j]); indegree[j]++; } } } Toposort(); while(t--) { scanf("%d %d", &u, &v); if(scc[u] == scc[v]) printf("0\n"); else { u = scc[u]; v = scc[v]; for(int i = 1; i <= scc_cnt; i++) dis[i] = INF; dis[u] = 0; for(int i = 0; i < iq; i++) { for(int k = head1[que[i]]; k != -1; k = edge1[k].next) { if(dis[edge1[k].v] > dis[que[i]]+edge1[k].w) dis[edge1[k].v] = dis[que[i]]+edge1[k].w; } } if(dis[v] != INF) printf("%d\n", dis[v]); else printf("Nao e possivel entregar a carta\n"); } } printf("\n"); } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int u, v, w; while(cin>>n>>m) { if(n <= 0) break; edge_cnt = 0; memset(edge, 0, sizeof(edge)); memset(head, -1, sizeof(head)); for(int i = 0; i < m; i++) { scanf("%d %d %d", &u, &v, &w); add_edge(u, v, w); } scanf("%d", &t); solve(); } return 0; } </span>