您的位置:首页 > 其它

POJ-3114 Countries in War (强连通分量[Tarjan]&&Dijkstra)

2016-04-26 14:16 351 查看
Countries in War
http://poj.org/problem?id=3114

Time Limit: 1000MSMemory Limit: 65536K
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


题目大意:给定一个带权有向图,若u、v互相可达,则u、v之间可0花费(与权值无关)互相可达,给出k个查询,求起点到终点的最短距离?

由于互相可达的点是0花费,所以将强连通分量缩成一个点,就变成一道裸的最短路,再跑一边Dijkstra即可

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int MAXN=505;

struct Node {
int e,w;

Node(int ee=0,int ww=0):e(ee),w(ww) {}

bool operator < (const Node& a) const {
return w>a.w;
}
}u;

int n,m,num,cnt,top,tot;
int stak[MAXN];
int low[MAXN],dfn[MAXN],color[MAXN];//biocks[i]表示删掉i点后,能形成的连通块数
vector<Node> g[MAXN],mp[MAXN];
priority_queue<Node> q;
bool isIn[MAXN];

void Tarjan(int u) {
stak[++top]=u;
isIn[u]=true;
dfn[u]=low[u]=++num;
int v;
for(int i=0;i<g[u].size();++i) {
v=g[u][i].e;
if(dfn[v]==0) {
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(isIn[v]&&dfn[v]<low[u])
low[u]=dfn[v];
}

if(dfn[u]==low[u]) {
++cnt;
do {
v=stak[top--];
isIn[v]=false;
color[v]=cnt;//缩点
} while(v!=u);
}
}

int Dijkstra(int sta,int des) {
memset(isIn,false,sizeof(isIn));
while(!q.empty()) {
q.pop();
}
q.push(Node(sta,0));

while(!q.empty()) {
u=q.top();
q.pop();
if(!isIn[u.e]) {
if(u.e==des)//找到终点则返回其最短路
return u.w;
isIn[u.e]=true;
for(int i=0;i<mp[u.e].size();++i)
q.push(Node(mp[u.e][i].e,u.w+mp[u.e][i].w));
}
}
return -1;//若无法到达终点,则返回-1
}

int main() {
int s,e,w,k,ans;
while(scanf("%d%d",&n,&m)==2&&n!=0) {
for(int i=1;i<=n;++i) {
g[i].clear();
mp[i].clear();
dfn[i]=0;
isIn[i]=false;
}
while(m-->0) {
scanf("%d%d%d",&s,&e,&w);
g[s].push_back(Node(e,w));
}

tot=cnt=num=top=0;
for(int i=1;i<=n;++i) {
if(dfn[i]==0)
Tarjan(i);
}
for(int i=1;i<=n;++i) {
for(int j=0;j<g[i].size();++j) {
if(color[i]!=color[g[i][j].e]) {
mp[color[i]].push_back(Node(color[g[i][j].e],g[i][j].w));//构建新图
}
}
}

scanf("%d",&k);
while(k-->0) {
scanf("%d%d",&s,&e);
ans=Dijkstra(color[s],color[e]);
if(ans==-1)
printf("Nao e possivel entregar a carta\n");
else
printf("%d\n",ans);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: