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

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

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

2019-11-11 04:00:26
字體:
來源:轉載
供稿:網友
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
久久久女人电视剧免费播放下载| 97在线精品视频| 色悠悠久久久久| 亚洲成人免费网站| 久久精品国产2020观看福利| 欧美乱大交xxxxx另类电影| 91精品国产自产91精品| 北条麻妃99精品青青久久| 日韩精品在线免费观看视频| 最近的2019中文字幕免费一页| 国产精品午夜视频| 欧美性xxxx18| 日本久久久久亚洲中字幕| 一区二区三区国产视频| 2019亚洲日韩新视频| 日本精品视频在线观看| 最近日韩中文字幕中文| 91精品久久久久久久久久久久久| 欧美日韩亚洲一区二| 91热精品视频| 中文字幕亚洲无线码a| 久久伊人免费视频| 日韩欧美在线观看视频| 国产精品99久久久久久久久| 国产一区二区三区在线免费观看| 2025国产精品视频| 国产精品久久久久久婷婷天堂| 欧美日韩国产黄| 在线播放日韩欧美| 日韩激情第一页| 日本一区二三区好的精华液| 三级精品视频久久久久| 亚洲一区国产精品| 欧美日韩激情视频| 日本电影亚洲天堂| 国产精品久久色| 国产精自产拍久久久久久蜜| 欧美日韩亚洲精品一区二区三区| 91九色国产社区在线观看| 日韩国产激情在线| 国产精品青草久久久久福利99| 精品欧美激情精品一区| 欧美午夜www高清视频| 日韩免费在线免费观看| 亚洲最大福利视频网站| 欧美精品videosex性欧美| 久久久人成影片一区二区三区| 亚洲国产日韩精品在线| 国产成人精品优优av| 精品久久久国产精品999| 青青a在线精品免费观看| 啪一啪鲁一鲁2019在线视频| 55夜色66夜色国产精品视频| 久久夜色精品国产| 国产精品视频色| 色婷婷av一区二区三区在线观看| 国产在线不卡精品| 中文字幕视频在线免费欧美日韩综合在线看| 国产亚洲视频在线| 国产欧美日韩免费看aⅴ视频| 国产精品va在线播放我和闺蜜| 91情侣偷在线精品国产| 日韩免费高清在线观看| 在线电影中文日韩| 亚洲欧美国内爽妇网| 国产精品96久久久久久又黄又硬| 日韩av网站导航| 秋霞av国产精品一区| 欧美视频不卡中文| xxx成人少妇69| 97精品久久久中文字幕免费| 日韩电影中文字幕一区| 26uuu亚洲国产精品| 色婷婷综合成人av| 久久99久久久久久久噜噜| 中文字幕日韩免费视频| 久久视频在线视频| 成人精品视频久久久久| 国产欧美精品在线| 中文字幕av一区| 欧美亚洲国产精品| 欧美精品一区二区三区国产精品| 久久影院模特热| 91在线|亚洲| 在线播放精品一区二区三区| 91国内揄拍国内精品对白| 亚洲色图国产精品| 亚洲福利小视频| 92看片淫黄大片看国产片| 97超级碰碰碰久久久| 成人xvideos免费视频| 色综合天天综合网国产成人网| 最近中文字幕mv在线一区二区三区四区| 精品久久久久久电影| 欧美极品美女视频网站在线观看免费| 精品偷拍一区二区三区在线看| 热久久免费视频精品| 国产精品视频精品视频| 久久99久国产精品黄毛片入口| 亚洲色图综合久久| 国产999精品久久久影片官网| 中文字幕精品一区二区精品| 亚洲一区中文字幕| 日韩暖暖在线视频| 色综合久久88色综合天天看泰| xvideos亚洲| 国产精品入口夜色视频大尺度| 国产69精品久久久| 日韩av日韩在线观看| 精品久久久久久| 中文字幕国产亚洲| 久久免费视频在线| 欧美另类第一页| 黄色成人在线免费| 疯狂蹂躏欧美一区二区精品| 91精品国产91久久| 久久精品欧美视频| 亚洲经典中文字幕| 欧美激情亚洲国产| 国产精品视频公开费视频| 91精品国产综合久久久久久蜜臀| 欧美性猛交99久久久久99按摩| 国内精品免费午夜毛片| 国产脚交av在线一区二区| 日韩一级黄色av| 精品香蕉在线观看视频一| 国产精品青草久久久久福利99| 国产精品18久久久久久麻辣| 538国产精品一区二区免费视频| 北条麻妃一区二区在线观看| 乱亲女秽乱长久久久| 欧美在线观看网址综合| 国内精品久久影院| 欧美大尺度电影在线观看| 成人激情在线播放| 国产香蕉97碰碰久久人人| 欧美日本高清视频| 亚洲国产高清自拍| 国产成人高清激情视频在线观看| 欧美日韩ab片| 91av免费观看91av精品在线| 精品久久久久久中文字幕大豆网| 97香蕉超级碰碰久久免费的优势| 国产精品久久久久久久久久久久久| 国产精品一区久久| 一级做a爰片久久毛片美女图片| 国产亚洲激情在线| 91经典在线视频| 日韩av影片在线观看| 成人久久久久久久| 久久久综合免费视频| 国产精品久久色| 久久精品成人一区二区三区| 亚洲欧美日韩网| 日韩最新免费不卡| 日韩欧亚中文在线| 亚洲国产精品久久久久| 日本久久久久久久久久久| 欧美成人在线免费| 国产噜噜噜噜噜久久久久久久久| 成人亚洲综合色就1024| 精品视频久久久久久久| 精品无人区太爽高潮在线播放| 91高潮在线观看|