亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

POJ 3114 Tarjan + 最短路(三種姿勢均能過)

2019-11-11 05:28:55
字體:
來源:轉載
供稿:網友
Countries in War
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3808 Accepted: 1105

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, XY and H (1 ≤ XY ≤ 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 ≤ OD ≤ 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 51 2 52 1 103 4 84 3 72 3 651 21 31 44 34 13 31 2 102 3 13 2 131 33 13 20 0

Sample Output

0660Nao e possivel entregar a carta10Nao e possivel entregar a carta0

思路:Tarjan縮點后跑個最短路就行,剛好很久沒寫過最短路了,就三種寫法都復習了一遍,最后提交情況如圖:

 Dijkstra + 堆優化 : 219ms

 spfa                      :   266ms

Floyd                     :   1000ms

代碼:

Dijkstra + 堆優化:

#include <iostream>#include <cstdio>#include <cstdlib>#include<cstring>#include <string>#include <algorithm>#include <cmath>#include <cctype>#include<queue>#include<map>#include<stack>using namespace std;typedef long long ll;const int INF = 1e8;const int maxn = 550;struct Node{	int dis,id;	friend bool Operator < (Node a,Node b){		return a.dis > b.dis;	}}ans[maxn];struct P{	int u,v,w;	int next;}G1[maxn*maxn],G2[maxn*maxn];int head_1[maxn],head_2[maxn];int vis[maxn],dfn[maxn],low[maxn],point[maxn];int n,m,num,cnt,tot;stack<int> S;priority_queue<Node> que;void init(void){	num = cnt = tot = 0;	memset(head_1,-1,sizeof(head_1));	memset(head_2,-1,sizeof(head_2));	for(int i=0 ;i<=n ;i++){		vis[i] = dfn[i] = low[i] = point[i] = 0;	}}void add_G1(int u,int v,int w){	G1[cnt].u = u;	G1[cnt].v = v;	G1[cnt].w = w;	G1[cnt].next = head_1[u];	head_1[u] = cnt++;}void add_G2(int u,int v,int w){	G2[cnt].u = u;	G2[cnt].v = v;	G2[cnt].w = w;	G2[cnt].next = head_2[u];	head_2[u] = cnt++;}void Tarjan(int x){	dfn[x] = low[x] = tot++;	vis[x] = 1;	S.push(x);	for(int i=head_1[x] ; i != -1; i = G1[i].next){		int v = G1[i].v;		if(!dfn[v]){			Tarjan(v);			low[x] = min(low[x],low[v]);		}		else if(vis[v]){			low[x] = min(low[x],dfn[v]);		}		}	if(dfn[x] == low[x]){		num++;		while(1){			int tem = S.top();			S.pop();			vis[tem] = 0;			point[tem] = num;			if(tem == x)	break;		}	}}void Dijkstra(int s){	while(que.size())	que.pop();	memset(vis,0,sizeof(vis));	for(int i=1 ;i<=num ;i++){		ans[i].dis = INF;		ans[i].id = i;		}	ans[s].dis = 0;	que.push(ans[s]);	while(que.size()){		Node u = que.top();		que.pop();		if(vis[u.id])	continue;		vis[u.id] = 1;		for(int i=head_2[u.id] ;i!=-1 ;i = G2[i].next){			int v = G2[i].v;			if(u.dis + G2[i].w < ans[v].dis && (!vis[v])){				ans[v].dis = u.dis + G2[i].w;				que.push(ans[v]);			}			}		}}int main(){	while(scanf("%d%d",&n,&m) != EOF && n){		init();		while(m--){			int u,v,w;			scanf("%d%d%d",&u,&v,&w);			add_G1(u,v,w);		}		for(int i=1 ;i<=n ;i++){			if(!dfn[i])	Tarjan(i);		}		cnt = 0;		for(int i=1 ;i<=n ;i++){			for(int j=head_1[i] ;j != -1 ;j = G1[j].next){				int v = G1[j].v;				if(point[v] != point[i]){					add_G2(point[i],point[v],G1[j].w);				}			}		}		int Q;		scanf("%d",&Q);		while(Q--){			int u,v;			scanf("%d%d",&u,&v);			Dijkstra(point[u]);			int w = ans[point[v]].dis;			if(w == INF)				printf("Nao e possivel entregar a carta/n");			else				printf("%d/n",w);			}		printf("/n");	}	return 0;}

Floyd:

#include <iostream>#include <cstdio>#include <cstdlib>#include<cstring>#include <string>#include <algorithm>#include <cmath>#include <cctype>#include<queue>#include<map>#include<stack>using namespace std;typedef long long ll;const int INF = 1e8;const int maxn = 550;struct P{	int u,v,w;	int next;}G1[maxn*maxn];int G2[maxn][maxn];int head_1[maxn],head_2[maxn];int vis[maxn],dfn[maxn],low[maxn],dis[maxn],point[maxn];int n,m,num,cnt,tot;stack<int> S;queue<int> que;void init(void){	num = cnt = tot = 0;	memset(head_1,-1,sizeof(head_1));	memset(head_2,-1,sizeof(head_2));	for(int i=0 ;i<=n ;i++){		vis[i] = dfn[i] = low[i] = point[i] = 0;	}}void add_G1(int u,int v,int w){	G1[cnt].u = u;	G1[cnt].v = v;	G1[cnt].w = w;	G1[cnt].next = head_1[u];	head_1[u] = cnt++;}void Tarjan(int x){	dfn[x] = low[x] = tot++;	vis[x] = 1;	S.push(x);	for(int i=head_1[x] ; i != -1; i = G1[i].next){		int v = G1[i].v;		if(!dfn[v]){			Tarjan(v);			low[x] = min(low[x],low[v]);		}		else if(vis[v]){			low[x] = min(low[x],dfn[v]);		}		}	if(dfn[x] == low[x]){		num++;		while(1){			int tem = S.top();			S.pop();			vis[tem] = 0;			point[tem] = num;			if(tem == x)	break;		}	}}void Floyd(void){	for(int k=1 ;k<=num ;k++){		for(int i=1 ;i<=num ;i++){			for(int j=1 ;j<=num ;j++){				if(G2[i][k] + G2[k][j] < G2[i][j])					G2[i][j] = 	G2[i][k] + G2[k][j];			}		}	}}int main(){	while(scanf("%d%d",&n,&m) != EOF && n){		init();		while(m--){			int u,v,w;			scanf("%d%d%d",&u,&v,&w);			add_G1(u,v,w);		}		for(int i=1 ;i<=n ;i++){			if(!dfn[i])	Tarjan(i);		}		cnt = 0;		for(int i=1 ;i<=num ;i++){			for(int j=1 ;j<=num ;j++){				if(i == j)					G2[i][j] = 0;				else					G2[i][j] = INF;			}		}		for(int i=1 ;i<=n ;i++){			for(int j=head_1[i] ;j != -1 ;j = G1[j].next){				int v = G1[j].v;				if(point[v] != point[i]){					G2[point[i]][point[v]] = min(G2[point[i]][point[v]],G1[j].w);				}			}		}		Floyd();		int Q;		scanf("%d",&Q);		while(Q--){			int u,v;			scanf("%d%d",&u,&v);			int w = G2[point[u]][point[v]];			if(w == INF)				printf("Nao e possivel entregar a carta/n");			else				printf("%d/n",w);			}		printf("/n");	}	return 0;}

Spfa:

#include <iostream>#include <cstdio>#include <cstdlib>#include<cstring>#include <string>#include <algorithm>#include <cmath>#include <cctype>#include<queue>#include<map>#include<stack>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int maxn = 550;struct P{	int u,v,w;	int next;}G1[maxn*maxn],G2[maxn*maxn];int head_1[maxn],head_2[maxn];int vis[maxn],dfn[maxn],low[maxn],dis[maxn],point[maxn];int n,m,num,cnt,tot;stack<int> S;queue<int> que;void init(void){	num = cnt = tot = 0;	memset(head_1,-1,sizeof(head_1));	memset(head_2,-1,sizeof(head_2));	for(int i=0 ;i<=n ;i++){		vis[i] = dfn[i] = low[i] = point[i] = 0;	}}void add_G1(int u,int v,int w){	G1[cnt].u = u;	G1[cnt].v = v;	G1[cnt].w = w;	G1[cnt].next = head_1[u];	head_1[u] = cnt++;}void add_G2(int u,int v,int w){	G2[cnt].u = u;	G2[cnt].v = v;	G2[cnt].w = w;	G2[cnt].next = head_2[u];	head_2[u] = cnt++;}void Tarjan(int x){	dfn[x] = low[x] = tot++;	vis[x] = 1;	S.push(x);	for(int i=head_1[x] ; i != -1; i = G1[i].next){		int v = G1[i].v;		if(!dfn[v]){			Tarjan(v);			low[x] = min(low[x],low[v]);		}		else if(vis[v]){			low[x] = min(low[x],dfn[v]);		}		}	if(dfn[x] == low[x]){		num++;		while(1){			int tem = S.top();			S.pop();			vis[tem] = 0;			point[tem] = num;			if(tem == x)	break;		}	}}int spfa(int u,int v){	memset(vis,0,sizeof(vis));	memset(dis,INF,sizeof(vis));	while(que.size())	que.pop();	dis[u] = 0;	vis[u] = 1;	que.push(u);	while(que.size()){		int tem = que.front();		que.pop();		vis[tem] = 0;		for(int i = head_2[tem] ;i != -1 ;i = G2[i].next){			int p = G2[i].v;			if(dis[p] > dis[tem] + G2[i].w){				dis[p] = dis[tem] + G2[i].w;				if(!vis[p]){					vis[p] = 1;					que.push(p);				}			}			}	}	return dis[v];}int main(){	while(scanf("%d%d",&n,&m) != EOF && n){		init();		while(m--){			int u,v,w;			scanf("%d%d%d",&u,&v,&w);			add_G1(u,v,w);		}		for(int i=1 ;i<=n ;i++){			if(!dfn[i])	Tarjan(i);		}		cnt = 0;		for(int i=1 ;i<=n ;i++){			for(int j=head_1[i] ;j != -1 ;j = G1[j].next){				int v = G1[j].v;				if(point[v] != point[i]){					add_G2(point[i],point[v],G1[j].w);				}			}		}		int Q;		scanf("%d",&Q);		while(Q--){			int u,v;			scanf("%d%d",&u,&v);			int w = spfa(point[u],point[v]);			if(w == INF)				printf("Nao e possivel entregar a carta/n");			else				printf("%d/n",w);			}		printf("/n");	}	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
热久久免费国产视频| 成人妇女免费播放久久久| 国产精品电影网站| 久久精品国产清自在天天线| 亚洲第一福利在线观看| 久久久人成影片一区二区三区观看| 国产精品久久久精品| 日韩av在线影视| 日韩av综合网站| 深夜福利一区二区| 久久这里有精品视频| 国产精品69av| 亚洲第一视频在线观看| 亚洲成人精品视频| 日韩中文字幕在线| 日本久久久久久久久| 国产亚洲激情在线| 久久久亚洲天堂| 青青久久av北条麻妃海外网| 亚洲欧美www| 国产福利精品av综合导导航| 性欧美xxxx| 精品久久久精品| 欧美最猛性xxxx| 亚洲一区美女视频在线观看免费| 久久精品国产一区二区三区| 国产精品96久久久久久| 国产精品嫩草影院一区二区| 亚洲天堂免费在线| 亚洲图片在线综合| 久青草国产97香蕉在线视频| 国产性色av一区二区| 国产精品久久久久久一区二区| 5566日本婷婷色中文字幕97| 久久久亚洲福利精品午夜| 深夜成人在线观看| 久久视频这里只有精品| 日韩国产精品视频| 成人免费视频97| 亚洲欧美国内爽妇网| 国产精品夫妻激情| 欧美成人激情视频免费观看| 97欧美精品一区二区三区| 一区二区三区黄色| 欧美综合在线第二页| 亚洲网在线观看| 久久久久五月天| 欧美黑人性猛交| 亚洲视频国产视频| 精品久久久久久亚洲国产300| 中文字幕欧美在线| 国产精品自拍视频| 久久久久久久久爱| 久久中文字幕国产| 欧美一级在线亚洲天堂| 国产成人精品视| 红桃av永久久久| 欧美在线视频网| 国产精自产拍久久久久久蜜| 精品夜色国产国偷在线| 最新国产精品亚洲| 在线成人中文字幕| 成人性生交xxxxx网站| 久久国产精彩视频| 国产xxx69麻豆国语对白| 欧美大片在线免费观看| 亚洲最大的网站| 亚洲国产另类 国产精品国产免费| 国产日产亚洲精品| 中文字幕在线看视频国产欧美在线看完整| 日韩欧美高清视频| 欧美大片欧美激情性色a∨久久| 日本精品免费一区二区三区| 78色国产精品| 久久久精品国产网站| 久久久久久12| 国产成人精品优优av| 亚洲xxxxx性| 91精品国产91久久久久久久久| 亚洲精品美女久久| 97精品国产97久久久久久免费| 亚洲第五色综合网| 这里只有视频精品| 91最新国产视频| 国产精品久久久久影院日本| 国产成人鲁鲁免费视频a| 美女少妇精品视频| 97超碰蝌蚪网人人做人人爽| 69国产精品成人在线播放| 亚洲欧美日本另类| 一区二区三区亚洲| 日韩欧美在线字幕| 欧美精品久久久久a| 亚洲精品电影在线| 精品av在线播放| 91精品国产色综合| 久久精品青青大伊人av| 欧美激情国产日韩精品一区18| 日韩一区视频在线| 久久99热这里只有精品国产| 亚洲国产精品久久久久秋霞蜜臀| 亚洲成人黄色网址| 国产精品视频网| 国产一区二区在线免费视频| 亚洲精品98久久久久久中文字幕| 最新日韩中文字幕| 欧美综合在线观看| 九色精品免费永久在线| 91国内在线视频| 北条麻妃在线一区二区| 日韩高清电影免费观看完整版| 欧美激情精品在线| 欧美网站在线观看| 国产99久久精品一区二区 夜夜躁日日躁| 国产成人精品久久久| 久久伊人91精品综合网站| 日本精品免费一区二区三区| 国产欧美婷婷中文| 亚洲最新中文字幕| 欧美日韩免费看| 欧美另类69精品久久久久9999| 中文字幕成人在线| 亚洲国产精品va在线观看黑人| 中文字幕亚洲第一| 亚洲精品美女久久| 欧美日韩在线第一页| 日韩在线视频线视频免费网站| 国产一区二区三区视频免费| 色综合天天狠天天透天天伊人| 欧美丰满片xxx777| 高清亚洲成在人网站天堂| 日韩午夜在线视频| 日韩在线免费高清视频| 国产日韩精品入口| 久久久久久999| 久久视频在线视频| 精品视频在线播放| 中文字幕亚洲欧美日韩高清| 久久久久久久成人| 久久久久亚洲精品| 亚洲成色777777在线观看影院| 欧美成人午夜激情| 热re91久久精品国99热蜜臀| 久久精品这里热有精品| 日韩精品视频免费在线观看| www.亚洲成人| 91久久久久久久| 91成人精品网站| 亚洲精品少妇网址| 久久免费视频网| 国产在线观看一区二区三区| 欧美日韩美女在线| 日韩欧美在线看| 精品无人区乱码1区2区3区在线| 97视频在线看| 深夜福利一区二区| 日韩免费观看网站| 尤物yw午夜国产精品视频明星| 成人淫片在线看| 国产一区私人高清影院| 超碰97人人做人人爱少妇| 亚洲女人天堂成人av在线| 国产精品专区h在线观看| 久久亚洲综合国产精品99麻豆精品福利|