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,那么我们 ...
随机推荐
- 戴文的Linux内核专题:03驱动程序
转自Linux中国 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协议(内核实际上先发送指令给BIOS,然后BIOS传给硬件). Lin ...
- C++中的容器类详解
一.STL容器类 STL(Standard Template Library)的六大组件:容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adap ...
- 多功能节点连线绘图控件Nevron Diagram for .NET使用方法及下载地址
Nevron Diagram for .NET是一个功能强大,世界上顶级的.NET图表控件.可扩展的图形报表构架,可以帮您创建功能丰富的Winforms及Webforms图表解决方案.这个产品构建于N ...
- maven的入门hello world
编写pom pom.xml是maven的核心.Project Object Model,定义了项目的基本信息,用于描述项目如何构建,项目依赖等. 首先,新建hello-world文件夹,新建pom.x ...
- eclipse 工程乱码问题
一 设置工作空间的编码格式(对新建的工程有效) window-->preference-->Gerneral-->Workspace-->Text file coding--- ...
- SharePoint开发 - 自定义导航菜单(二)母版页的菜单应用
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 本篇叙述在母版页中应用之前的配置信息生成菜单,主要涉及到母版页的自定义,并应用了第三方控件库DevExpress ...
- CSS网页布局:网站整体居中 转
body{ margin:0 auto; padding:0 auto; text-align:center;} 只设置body属性对ie浏览器有效果,但是对火狐等不起作用 body{ ...
- MongoDB应用篇(转)
一.高级查询 1. 查询操作符 1.1 比较操作符$gt,$lt,$gte,$lte 实例: select * from things where field<value -- 等价于db.th ...
- 理解Mach Port
参考文档: 1. http://robert.sesek.com/thoughts/2012/1/debugging_mach_ports.html 2. Mach 3 Kernel Interfac ...
- Python学习路程day6
shelve 模块 shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve d = shelve.open ...