Countries in War
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3672   Accepted: 1057

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 ≤ EN2), 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, YN, 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, DN). 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
【分析】给出一些城市之间的有向路,如果两个城市间互相连通,则说明这两个城市属于同一国家,传递邮件不需要时间。不在同一国家则需要时间。
给出一些询问,输出俩城市间的最短路,若不能到达,则输出那句英文。
先用Tarjan缩点,再Spfa.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int s,t,n,m,cnt,tim,top,cut;
int head[N],dfn[N],low[N],stack1[N],dis[N];
int num[N],in[N],out[N],vis[N],w[N][N],mp[N][N]; struct man {
int to,nxt;
} edg[M];
void addedg(int u,int v) {
edg[cnt].to=v;
edg[cnt].nxt=head[u];
head[u]=cnt++;
}
void init() {
cnt=;
tim=;
top=cut=;
memset(head,-,sizeof head);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(stack1,,sizeof stack1);
memset(num,,sizeof num);
memset(in,,sizeof in);
memset(out,,sizeof out);
memset(vis,,sizeof vis);
memset(edg,,sizeof edg);
memset(w,inf,sizeof w);
memset(mp,inf,sizeof mp);
memset(dis,inf,sizeof dis); }
void dfs(int u) {
int v;
low[u] = dfn[u] = ++tim;
stack1[top++] = u;
vis[u] = ;
for(int e = head[u]; e != -; e = edg[e].nxt) {
v = edg[e].to;
if(!dfn[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if(vis[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]) {
cut++;//printf("!!!%d\n",u);
do {
v = stack1[--top];
num[v] = cut;
vis[v] = ;
} while(u != v);
}
}
void Spfa(int s) {
memset(vis,,sizeof vis);
memset(dis,inf,sizeof dis);
dis[s]=;
queue<int>q;
q.push(s);
vis[s]=;
while(!q.empty()) {
int t=q.front();
q.pop();
vis[t]=;
for(int i=; i<=cut; i++) {
if(dis[i]>dis[t]+mp[t][i]) {
dis[i]=dis[t]+mp[t][i];
if(!vis[i]) {q.push(i);vis[i]=;}
}
}
}
}
int main() {
while(~scanf("%d%d",&n,&m)&&n!=) {
init();
int a,b,c;
for(int i=; i<=m; i++) {
scanf("%d%d%d",&a,&b,&c);
addedg(a,b);
w[a][b]=min(w[a][b],c);
}
for(int i=; i<=n; i++)if(!dfn[i])dfs(i);
for(int i=;i<=n;i++)mp[i][i]=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i!=j&&num[i]!=num[j]&&w[i][j]!=inf)mp[num[i]][num[j]]=min(mp[num[i]][num[j]],w[i][j]);
}
}
int k;
scanf("%d",&k);
for(int i=; i<k; i++) {
scanf("%d%d",&a,&b);
if(num[a]==num[b]) {
puts("");
} else {
Spfa(num[a]);
if(dis[num[b]]<inf)printf("%d\n",dis[num[b]]);
else puts("Nao e possivel entregar a carta");
}
}
printf("\n");
}
return ;
}

POJ 3114 Countries in War(强连通)(缩点)(最短路)的更多相关文章

  1. POJ 3114 Countries in War(强连通+最短路)

    POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...

  2. POJ 3114 Countries in War(强联通分量+Tarjan)

    题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include ...

  3. poj 3114 Countries in War

    http://poj.org/problem?id=3114 #include <cstdio> #include <cstring> #include <queue&g ...

  4. Countries in War -POJ3114Tarjan缩点+SPFA

    Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...

  5. Countries in War(强连通分量及其缩点)

    http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...

  6. POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)

    题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...

  7. POJ 1236 学校传数据 强连通+缩点+DAG

    题意描述: 网络中有一些学校,每个学校可以分发软件给其他学校.可以向哪个分发取决于他们各自维护的一个清单. 两个问题 1:至少要copy多少份新软件给那些学校, 才能使得每个学校都能得到. 2:要在所 ...

  8. poj 2553强连通+缩点

    /*先吐槽下,刚开始没看懂题,以为只能是一个连通图0T0 题意:给你一个有向图,求G图中从v可达的所有点w,也都可以达到v,这样的v称为sink.求这样的v. 解;求强连通+缩点.求所有出度为0的点即 ...

  9. BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...

随机推荐

  1. java的动态绑定和静态绑定

    首先是方法的参数是父类对象,传入子类对象是否可行然后引出Parent p = new Children();这句代码不是很理解,google的过程中引出向上转型要理解向上转型又引出了动态绑定从动态绑定 ...

  2. S5PV210之添加缺少的-内核提供的'.h'文件 linux3.0.8驱动

    怎样解决编译时出现内核提供的函数或变量没有定义,使用source insight搜索功能找到声明的头文件,然后包含该头件就行了: 比如: error: implicit declaration of ...

  3. [C/C++]C++标准中的名词

    1.qualified-id.nested-name-specifier: [example: struct A { struct B { void F(); }; }; A is an unqual ...

  4. [windows操作系统]windows模块

    smss.exe csrss.exe    Client/Server Runtime Server Subsystem

  5. SharePoint 2013 Nintex Workflow 工作流帮助(七)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 11. Check out item(Libraries and lists分组) 与上一个对应,用于签出条目.如果一个 ...

  6. 直接拿来用的10个PHP代码片段

    PHP是一种HTML内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言.PHP拥有数以百计的基本功能,支持上千种扩展.这些功能都被很好的加载在PHP站点上,但内置的库有各种各样的命名.在P ...

  7. alloc和初始化的定义

    1.alloc是为原始实例进行分配内存,但是还不能使用 2.初始化的作用就是将一个对象的初始状态(即它的实例变量和属性)设定为合理的值,然后返回对象.它的目的就是返回一个有用的值

  8. HDU 5009

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题意:一个数列,每个点代表一种颜色,每次选一个区间覆盖,覆盖的代价是区间内颜色种类数的平方,直到覆盖整个数 ...

  9. JQuery 实现倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. IOS的MVC

    1 翻牌游戏 1.1 问题 根据苹果MVC设计模式的思想原则实现一个简单的翻牌游戏,功能如下: 1)界面上随机摆放12张背面朝上的纸牌,界面效果如图-1所示: 图- 1 2)点击纸牌可以使纸牌翻页,翻 ...