POJ 3114 Countries in War 强连通tarjan缩点后 跑最短路spfa
2014-08-02 22:08
405 查看
Countries in War
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
Sample Output
题目说 ,战时有n座城市,某些城市间可以单向传递信息. 如果, 某几个城市两两都可以相互传递信息,那代表他们是一个国家的.
输入的E代表可以连通 的城市有哪几对,X->Y 耗时H.
一个国家之间传递信息可以瞬间完成. 所以这是一个有向有环图. 通过tarjan 缩点. 去环后. 把每个国家,也就是每个强连通分量之间的最短距离求出.然后跑一变spfa,就可以得出两个点的最短传输时间了.
如果无法传输到输出 Nao e possivel entregar a carta;
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2359 | Accepted: 736 |
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
题目说 ,战时有n座城市,某些城市间可以单向传递信息. 如果, 某几个城市两两都可以相互传递信息,那代表他们是一个国家的.
输入的E代表可以连通 的城市有哪几对,X->Y 耗时H.
一个国家之间传递信息可以瞬间完成. 所以这是一个有向有环图. 通过tarjan 缩点. 去环后. 把每个国家,也就是每个强连通分量之间的最短距离求出.然后跑一变spfa,就可以得出两个点的最短传输时间了.
如果无法传输到输出 Nao e possivel entregar a carta;
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #include<vector> #include<queue> #define inf 1000000000 int min(int a,int b) { return a>b?b:a; } int max(int a,int b) { return a<b?b:a; } #define N 600 //N为最大点数 #define M 250000 //M为最大边数 int n, m;//n m 为点数和边数 struct Edge{ int from, to, nex, len; bool sign;//是否为桥 }edge[M<<1]; int head , edgenum; void add(int u, int v ,int len){//边的起点和终点 Edge E={u, v, head[u], len, false}; edge[edgenum] = E; head[u] = edgenum++; } int DFN , Low , Stack , top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳) int taj;//连通分支标号,从1开始 int Belong ;//Belong[i] 表示i点属于的连通分支 bool Instack ; vector<int> bcc ; //标号从1开始 void tarjan(int u ,int fa) { DFN[u] = Low[u] = ++ Time ; Stack[top ++ ] = u ; Instack[u] = 1 ; for (int i = head[u] ; ~i ; i = edge[i].nex ) { int v = edge[i].to; if(DFN[v] == -1) { tarjan(v , u); Low[u] = min(Low[u] ,Low[v]) ; if(DFN[u] < Low[v]) { edge[i].sign = 1;//为割桥 } } else if(Instack[v]) { Low[u] = min(Low[u] ,DFN[v]) ; } } if(Low[u] == DFN[u]) { int now; taj ++ ; bcc[taj].clear(); do{ now = Stack[-- top] ; Instack[now] = 0 ; Belong [now] = taj ; bcc[taj].push_back(now); }while(now != u) ; } } void tarjan_init(int all){ memset(DFN, -1, sizeof(DFN)); memset(Instack, 0, sizeof(Instack)); top = Time = taj = 0; for(int i=1;i<=all;i++) if(DFN[i]==-1 ) tarjan(i, i); //注意开始点标!!! } vector<int>G ; int du ; //入度 void suodian(){ memset(du, 0, sizeof(du)); for(int i = 1; i <= taj; i++) G[i].clear(); for(int i = 0; i < edgenum; i++) { int u = Belong[edge[i].from], v = Belong[edge[i].to]; if(u!=v) { G[u].push_back(v), du[v]++; } } } //最短路 int mp[600][600],dist[600];//对于原图中的每个点u,都属于新图中的一个新点Belong[u]; bool visit[600]; void spfa(int sta) //SPFA算法 //同一个点入队 大于等于点数. 就是有负环,自己写. { int i, now; memset(visit, false, sizeof(visit)); for(i = 1; i <= taj; i++) dist[i] = inf; dist[sta] = 0; queue<int> Q; Q.push(sta); visit[sta] = true; while(!Q.empty()) { now = Q.front(); Q.pop(); visit[now] = false; for(i = 1; i <= taj; i++)//以 now为中间点. 遍历一遍,看有没有更短的到i点的路. vis 为了更快,不重复更新. { if(dist[i] > dist[now] + mp[now][i]) { dist[i] = dist[now] + mp[now][i]; if(visit[i] == 0) { Q.push(i); visit[i] = true; } } } } } void init(){memset(head, -1, sizeof(head)); edgenum=0;} int main() { while(scanf("%d%d",&n,&m),n) { init(); int a,b,c; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) mp[i][j]=0; else mp[i][j]=inf; } } for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); } tarjan_init(n); suodian(); for(int i=0;i<m;i++) { if(mp[Belong[edge[i].from]][Belong[edge[i].to]]>edge[i].len) mp[Belong[edge[i].from]][Belong[edge[i].to]]=edge[i].len; } int q; scanf("%d",&q); while(q--) { scanf("%d%d",&a,&b); if(Belong[a]==Belong[b]) printf("0\n"); else { spfa(Belong[a]); if(dist[Belong[b]]==inf) puts("Nao e possivel entregar a carta"); else printf("%d\n",dist[Belong[b]]); } } puts(""); } return 0; }
相关文章推荐
- I - Instantaneous Transference POJ - 3592 ,spfa求最长路+tarjan缩点
- poj 2186 Popular Cows(Tarjan,强连通分量缩点)
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- pku 3114 Countries in War tarjan缩点+spfa求最短路
- Countries in War (POJ 3114) Tarjan缩点+最短路
- poj 3177 Redundant Paths 【无向图增加最少的边是图成为边—双连通】【tarjan求EBC + 缩点 统计度数为1的EBC】
- [POJ 3177]Redundant Paths[边双连通][Tarjan][缩点]
- POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
- POJ-3352 Road Construction,tarjan缩点求边双连通!
- POJ 1236 Network of Schools (tarjan求强连通,缩点)
- POJ 1236 Network of Schools(Tarjan Algorithm求强连通子集,缩点后DAG上出或入度为0的点)
- hrbust 1797 Fly【Tarjan强连通+缩点染色+最长路SPFA】好题~
- SDUT 3262 Circle of Friends【强连通Tarjan+缩点染色+最短路SPFA】
- BZOJ 1179: [Apio2009]Atm Tarjan强连通分量缩点,SPFA,DP
- POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)
- Network of Schools POJ - 1236 tarjan强连通分量缩点
- POJ 2186 -- Popular Cows【强连通分支 && Tarjan缩点】
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
- POJ2762 Going from u to v or from v to u? 强连通 Tarjan缩点+拓扑排序topsort
- POJ 3177--Redundant Paths【无向图增加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】