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

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

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

2019-11-11 04:01:52
字體:
來源:轉載
供稿:網友
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电影| 日本韩国欧美精品大片卡二| 日韩a**站在线观看| 国产一区玩具在线观看| 日韩av一区二区在线观看| 91国产精品91| 亚洲午夜小视频| 日本久久久a级免费| 欧美一区二区三区艳史| 91人人爽人人爽人人精88v| 国产精品欧美一区二区| 韩国美女主播一区| 911国产网站尤物在线观看| 九九久久国产精品| 国产日韩欧美一二三区| 高清一区二区三区四区五区| 欧美xxxx18国产| 欧美麻豆久久久久久中文| 欧美日韩国产一区在线| 成人激情综合网| 久久久久久久国产| 国产精品pans私拍| 高清在线视频日韩欧美| 成人免费视频97| 热re91久久精品国99热蜜臀| 日韩有码在线电影| 97视频免费在线看| 久久人人爽亚洲精品天堂| 亚洲天堂开心观看| 日韩av免费在线| 日本久久久久亚洲中字幕| 国产日本欧美一区二区三区在线| 欧美国产日韩一区二区在线观看| 欧美另类暴力丝袜| 欧美成人剧情片在线观看| 亚洲精品久久久久久久久久久久久| 亚洲成人网在线观看| 亚洲第一网站男人都懂| 91在线观看欧美日韩| 久久精品国产一区二区三区| 国产精品视频网址| 少妇高潮久久久久久潘金莲| 午夜免费日韩视频| 欧美成人激情在线| 91精品久久久久久久久久另类| 日本国产一区二区三区| 欧美视频国产精品| 成人情趣片在线观看免费| 欧美日韩午夜激情| 日韩成人在线视频| 亚洲人成伊人成综合网久久久| 91久久久久久国产精品| 色yeye香蕉凹凸一区二区av| 久久99国产精品自在自在app| 欧美成人免费网| 亚洲美女av在线| 欧美激情成人在线视频| 国产精品日韩在线| 国产视频精品va久久久久久| 亚洲第一网站男人都懂| 亚洲aⅴ日韩av电影在线观看| 亚洲丝袜av一区| 4p变态网欧美系列| 久久成年人视频| 亚洲精品一区中文| 性夜试看影院91社区| 国产精品久久久久福利| 91久久夜色精品国产网站| 有码中文亚洲精品| 一道本无吗dⅴd在线播放一区| 欧美一区二区三区……| 8x拔播拔播x8国产精品| 国产精品揄拍500视频| 色婷婷综合成人av| 久久久久国色av免费观看性色| 亚洲精品免费av| 日产精品久久久一区二区福利| 国产小视频91| 永久免费精品影视网站| 国产精品久久久久av免费| 亚洲第一区在线| 亚洲国产精品网站| 精品国产乱码久久久久久婷婷| 日韩二区三区在线| 欧美国产在线电影| 色综合亚洲精品激情狠狠| 国自产精品手机在线观看视频| 亚洲国内高清视频| 精品久久久久久久久久ntr影视| 久久久久久久久久久久久久久久久久av| 久久久久久久久久久成人| 国产精品91在线| 欧美成人国产va精品日本一级| 高清在线视频日韩欧美| 欧美亚洲在线观看| xvideos亚洲| 国产亚洲精品激情久久| 日韩欧美在线免费观看| 久久久久这里只有精品| 国产精品jvid在线观看蜜臀| 亚洲xxxx做受欧美| 日韩免费视频在线观看| 欧美与欧洲交xxxx免费观看| 国产精品欧美一区二区| 日韩欧美中文第一页| 尤物yw午夜国产精品视频| 成人a在线观看| 亚洲国产美女精品久久久久∴| 亚洲国产精品999| 日韩大陆欧美高清视频区| 美女福利视频一区| 国产午夜精品视频免费不卡69堂| 18性欧美xxxⅹ性满足| 国产精品专区第二| 久久亚洲精品国产亚洲老地址| 中文欧美在线视频| 2019中文字幕在线观看| 日韩大片在线观看视频| 日韩精品在线观| 色诱女教师一区二区三区| 久久综合色影院| 欧美高清在线观看| 色偷偷av亚洲男人的天堂| 欧美亚洲一区在线| 日韩欧美aⅴ综合网站发布| 国产精品精品视频| 精品视频久久久| 亚洲大胆人体视频| 国产一区二区三区四区福利| 国产精品第1页| 国产成人精品免费久久久久| 美日韩精品视频免费看| 久久九九国产精品怡红院| 国产激情视频一区| 国产成人精品一区二区在线| 国内自拍欧美激情| 久久影视电视剧免费网站| 欧美人在线视频| 亚洲白拍色综合图区| 91av福利视频| 91丝袜美腿美女视频网站| 欧美日韩国产综合视频在线观看中文| 久久久爽爽爽美女图片| 亚洲一区二区福利| 亚洲人成在线电影| 欧美国产亚洲精品久久久8v| 国产专区精品视频| 亚洲欧美激情另类校园| 日韩一二三在线视频播| 日韩精品免费在线视频观看| 国产精品夜间视频香蕉| 欧美整片在线观看| 91久久夜色精品国产网站| 欧美激情一区二区三区在线视频观看| 成人淫片在线看| 久久久成人精品| 国产精品观看在线亚洲人成网| 欧美性猛交丰臀xxxxx网站| 亚洲激情中文字幕| 成人国产精品免费视频| 国产一区二区三区中文| 欧美日韩在线另类|