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

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

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

2019-11-11 04:40: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
欧美国产日韩精品| 黑人巨大精品欧美一区二区免费| 久久伊人免费视频| 懂色aⅴ精品一区二区三区蜜月| 97人人模人人爽人人喊中文字| 精品久久久久久久大神国产| 国产精品美女无圣光视频| 午夜精品久久久久久99热软件| 日日狠狠久久偷偷四色综合免费| 亚洲女人天堂视频| 欧美孕妇性xx| 奇米4444一区二区三区| 最新国产精品亚洲| 91免费版网站入口| 中文字幕国产精品久久| 国产精品白嫩初高中害羞小美女| 亚洲自拍偷拍色片视频| 日韩小视频在线| 亚洲第一免费播放区| 欧美制服第一页| 欧美日韩免费区域视频在线观看| 国产欧亚日韩视频| 黄色成人在线播放| 亚洲视频电影图片偷拍一区| 亚洲自拍av在线| 欧美一级黑人aaaaaaa做受| 欧美视频二区36p| 日韩亚洲欧美中文高清在线| 亚洲人成在线观看网站高清| 韩国日本不卡在线| 国产精品一区二区在线| 久久久亚洲精品视频| 久久国产精品99国产精| 日韩网站在线观看| 国产激情综合五月久久| 秋霞午夜一区二区| 午夜精品一区二区三区在线视| 日韩成人中文字幕在线观看| 亚洲国产欧美自拍| 欧美日本国产在线| 久久免费国产视频| 91精品国产综合久久香蕉922| www高清在线视频日韩欧美| 欧美激情18p| 日本视频久久久| 午夜精品久久久久久99热| 亚洲精品美女久久久久| 欧美野外猛男的大粗鳮| 久久精品久久久久久国产 免费| 国产成人av在线播放| 国内精品免费午夜毛片| 久久久噜噜噜久久中文字免| 日韩av三级在线观看| 国产乱肥老妇国产一区二| 午夜伦理精品一区| 亚洲欧洲在线看| 欧美日韩国产在线播放| 精品网站999www| 日韩va亚洲va欧洲va国产| 57pao国产成人免费| 国产成人精品综合久久久| 一本大道亚洲视频| 久久天堂av综合合色| 一本色道久久综合亚洲精品小说| xvideos亚洲人网站| 中文字幕国产精品久久| 2023亚洲男人天堂| 欧美亚洲国产视频| 少妇激情综合网| 成人在线播放av| 久久久久久久电影一区| 18性欧美xxxⅹ性满足| 综合av色偷偷网| 色噜噜国产精品视频一区二区| 久久夜色精品国产亚洲aⅴ| 这里只有精品久久| 亚洲精品综合久久中文字幕| 亚洲欧美国产精品久久久久久久| 亚洲国产成人在线播放| 91av国产在线| 午夜精品福利视频| 国产精品久久久久久搜索| 亚洲欧美制服另类日韩| 成人激情免费在线| 亚洲色图欧美制服丝袜另类第一页| 国产精品夜间视频香蕉| 久久久久久国产精品三级玉女聊斋| 国产精品久久久久久av福利软件| 欧美猛交ⅹxxx乱大交视频| 国产精品小说在线| 国产日韩综合一区二区性色av| 日本国产一区二区三区| 国产91色在线播放| 热久久美女精品天天吊色| 色偷偷偷亚洲综合网另类| 热门国产精品亚洲第一区在线| 日韩激情视频在线| 亚洲国产成人一区| 亚洲视频在线观看| 日韩经典第一页| 综合网中文字幕| 亚洲欧美国产一区二区三区| 国产一区二区三区四区福利| 九九热最新视频//这里只有精品| 国产一区二区三区免费视频| 97在线日本国产| 日韩精品极品在线观看播放免费视频| 美女视频黄免费的亚洲男人天堂| 国产亚洲在线播放| 欧美性极品少妇精品网站| 国产精品网站大全| 两个人的视频www国产精品| 国产精品自产拍高潮在线观看| 全色精品综合影院| 亚洲欧美综合v| 国产精品美乳一区二区免费| 68精品久久久久久欧美| 国产精品视频内| 欧美日韩性视频在线| 精品国产老师黑色丝袜高跟鞋| 黑人巨大精品欧美一区二区一视频| 性色av一区二区三区| 午夜免费久久久久| 蜜臀久久99精品久久久久久宅男| 久久久91精品国产| 麻豆国产va免费精品高清在线| 成人亚洲欧美一区二区三区| 亚洲成人动漫在线播放| 欧美成人免费视频| 日韩成人av在线播放| 大伊人狠狠躁夜夜躁av一区| 亚洲情综合五月天| 国产精品成人一区二区三区吃奶| 欧美超级免费视 在线| 欧美极品少妇与黑人| 精品久久久久久国产91| 欧美电影在线观看高清| 91高清视频免费观看| 在线精品国产欧美| 国产精品自在线| 2020欧美日韩在线视频| 国产成人在线一区二区| 91最新在线免费观看| 在线观看欧美视频| 国产一区二区视频在线观看| 亚洲伦理中文字幕| 在线a欧美视频| 91精品啪在线观看麻豆免费| 久久av红桃一区二区小说| 在线观看欧美日韩| 国产精品成人va在线观看| 91免费看国产| 在线成人激情视频| 亚洲福利视频免费观看| 久久久最新网址| 欧美精品一区二区免费| 欧美黄色www| 亚洲国产精彩中文乱码av| 久久久久久一区二区三区| 精品视频在线播放色网色视频| 日韩精品免费在线观看| 久久久久九九九九| 欧美精品videos性欧美| 欧洲s码亚洲m码精品一区|