POJ 3114 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 ≤ 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, X, Y and H (1 ≤ X, Y ≤ 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 ≤ O, D ≤ 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 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(强连通)(缩点)(最短路)的更多相关文章
- POJ 3114 Countries in War(强连通+最短路)
POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...
- POJ 3114 Countries in War(强联通分量+Tarjan)
题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include ...
- poj 3114 Countries in War
http://poj.org/problem?id=3114 #include <cstdio> #include <cstring> #include <queue&g ...
- Countries in War -POJ3114Tarjan缩点+SPFA
Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...
- Countries in War(强连通分量及其缩点)
http://poj.org/problem?id=3114 题意:有n个城市,m条边,由a城市到b城市的通信时间为w,若a城市与b城市连通,b城市与a城市也连通,则a,b城市之间的通信时间为0,求出 ...
- POJ3114 Countries in War (强连通分量 + 缩点 + 最短路径 + 好题)
题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta ...
- POJ 1236 学校传数据 强连通+缩点+DAG
题意描述: 网络中有一些学校,每个学校可以分发软件给其他学校.可以向哪个分发取决于他们各自维护的一个清单. 两个问题 1:至少要copy多少份新软件给那些学校, 才能使得每个学校都能得到. 2:要在所 ...
- poj 2553强连通+缩点
/*先吐槽下,刚开始没看懂题,以为只能是一个连通图0T0 题意:给你一个有向图,求G图中从v可达的所有点w,也都可以达到v,这样的v称为sink.求这样的v. 解;求强连通+缩点.求所有出度为0的点即 ...
- BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...
随机推荐
- APP store 审核注意点
磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...
- Stm32_调试出现 Error:Flash Download Failed-"Cortex-M3"
rror:Flash Download Failed-"Cortex-M3"出现一般有两种情况: 1.SWD模式下,Debug菜单中,Reset菜单选项(Autodetect/HW ...
- 使用HttpOnly提升Cookie安全性
在介绍HttpOnly之前,我想跟大家聊聊Cookie及XSS. 随着B/S的普及,我们平时上网都是依赖于http协议完成,而Http是无状态的,即同一个会话的连续两个请求互相不了解,他们由最 ...
- 5月18日:top10面试算法-LRUcache的实现
问题描述: LRU算法:优先把那些最长时间没使用的对象置换掉.根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. JAVA实现: 测试: publ ...
- HTML中的边框属性
可以通过边框风格属性border-style设定上下左右边框的风格,该属性用于设置一个元素边框的样式,而且必须用于指定可见的边框.可以使用1到4个关键字,如果四个值都给出了,它们分别用于上.右.下和左 ...
- IOS 封装类的时候注释格式,使用的时候可以想官方库一样快捷显示
/** @brief 详情 @param 参数 @note 注意 @return 返回值类型 @code 这里写例题代码 @endcode @see 相似的方法参考 */
- Matrix_二维树状数组
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- 破解 crackme(“不可逆“函数)
系统 : Windows xp 程序 : crackme 程序下载地址 :http://pan.baidu.com/s/1i41oh9r 要求 : 注册机编写 使用工具 : IDA Pro & ...
- C++类的运用 和 三大函数
在<数据结构与算法分析C++描述>一书中给出了三段代码,简单描述了C++类的接口.实现.与调用: #ifndef INTCELL_H_INCLUDED #define INTCELL_H_ ...
- 为什么要urlencode
为什么要urlencode 1.为了正常获取值 字符 特殊字符的含义 URL编码 & 分隔不同的变量值对 %26 = 用来连接键和值 %3D ? 表示查询字符串的开始 %3F # 用来标志 ...