POJ 3114-Countries in War 强连通缩点+spfa最短路

Countries in War

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4890   Accepted: 1426

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 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

题意:

     有n个城市,m条单向边,边上的权值代表从该城市到目标城市的时间。现在给出q个询问,问从起点到终点要多少时间,注意,如果这两个城市在一个城市(可以互通),那么费用为0。

做法:

     强连通缩点,排除在同一个国家的情况,然后再用强连通的分块量跑最短路,记住,两个国家之间要存最短路,我的处理可能有点麻烦。就当留个板子吧。

     PS:图论的题目一做就是这样。。代码量吭哧吭哧就这么多了。。心塞。。


#include<stack>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const int maxn=1505;
int qiang,belong[maxn],cnt,dfn[maxn],low[maxn],n,m,q;
int mp[maxn][maxn],dist[maxn],head[maxn],now,vis[maxn],tremp[maxn][maxn];
vector<int> ve[maxn];
stack<int> sa;
struct node{
    int to,next,val;
}e[maxn*maxn];
void init(){
    cnt=0,qiang=0,now=0;
    memset(head,-1,sizeof(head));
    memset(mp,-1,sizeof(mp));
    memset(dfn,0,sizeof(dfn));
    mem(low,0); mem(tremp,-1);
    mem(belong,0);
    for(int i=1;i<=n;i++)
        ve[i].clear();
    while(!sa.empty()) sa.pop();
}
void build(){
    int x,y,z;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        if(mp[x][y]==-1) {
            mp[x][y]=z;
        }
        else mp[x][y]=min(mp[x][y],z);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(mp[i][j]!=-1){
                ve[i].push_back(j);
            }
        }
    }
}
void tarjan(int x){
    dfn[x]=low[x]=++cnt;
    sa.push(x);
    for(int i=0;i<ve[x].size();i++){
        int v=ve[x][i];
        if(!dfn[v]){
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(!belong[v]) low[x]=min(low[x],low[v]);
    }
    if(dfn[x]==low[x]){
        qiang++;
        while(1){
            int a=sa.top(); sa.pop();
            belong[a]=qiang;
            if(a==x) break;
        }
    }
}
void add(int u,int v,int w){
    e[now].to=v,e[now].next=head[u];
    e[now].val=w,head[u]=now++;
}
int spfa(int st,int en){
    for(int i=1;i<=qiang;i++){
        dist[i]=inf;
        vis[i]=0;
    }
    queue<int> q;
    dist[st]=0; vis[st]=1;
    q.push(st);
    while(!q.empty()){
        int now=q.front(); q.pop();
        vis[now]=0;
        for(int i=head[now];~i;i=e[i].next){
            int v=e[i].to,w=e[i].val;
            if(dist[v]>dist[now]+w){
                dist[v]=dist[now]+w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(dist[en]>=inf) return -1;
    return dist[en];
}
int main(){
    int x,y,z,cas=0;
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) break;
        init();
        build();
        for(int i=1;i<=n;i++)
            if(!dfn[i]) tarjan(i);
        for(int i=1;i<=n;i++){
            for(int j=0;j<ve[i].size();j++){
                int v=ve[i][j];
                int fx=belong[i],fy=belong[v];
                if(fx!=fy){
                    if(tremp[fx][fy]==-1) tremp[fx][fy]=mp[i][v];
                    else tremp[fx][fy]=min(mp[i][v],tremp[fx][fy]);
                }
            }
        }
        for(int i=1;i<=qiang;i++){
            for(int j=1;j<=qiang;j++){
                if(tremp[i][j]!=-1) add(i,j,tremp[i][j]);
            }
        }
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&x,&y);
            int xx=belong[x],yy=belong[y];
            if(xx==yy){
                printf("0\n");
                continue;
            }
            int ans=spfa(xx,yy);
            if(ans==-1) printf("Nao e possivel entregar a carta\n");
            else printf("%d\n",ans);
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81711883
今日推荐