POJ 3114 - Countries in War(强连通分量+缩点+拓扑排序+DAG最短路)

Countries in War

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d
& %I64u

Appoint description:

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

题意:间谍在战争时期想要传递一份邮件回国,邮件可以在各个邮局之间传播,但传递是单向的,并且耗时,如果两个邮局在一个国家的话,那么邮件在他们之间的传递不用耗时,判断两个邮局是否在一个国家的标准是两个邮局可以互相传递邮件。给出两个邮局,输出最短的到达时间。

思路:强连通分量+缩点+拓扑排序+DAG最短路。

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 510;
const int INF = 1<<30;
struct Edge
{
    int v;
    int w;
    int next;
} edge[MAXN*MAXN], edge1[MAXN*MAXN];
struct shrink_point
{
    int out;
    int in;
    int num;
} sp[MAXN];
int g[MAXN][MAXN];
int LOW[MAXN];
int DFN[MAXN];
int instack[MAXN];
int scc[MAXN];
int head[MAXN*MAXN];
int head1[MAXN*MAXN];
int indegree[MAXN];
int que[MAXN];
int dis[MAXN];
stack<int>Q;
int n, m, t, iq;
int edge_cnt;
int edge_cnt1;
int scc_cnt;
int dfs_cnt;

void add_edge(int u, int v, int w)
{
    edge[edge_cnt].v = v;
    edge[edge_cnt].w = w;
    edge[edge_cnt].next = head[u];
    head[u] = edge_cnt++;
}

void add_edge1(int u, int v, int w)
{
    edge1[edge_cnt1].v = v;
    edge1[edge_cnt1].w = w;
    edge1[edge_cnt1].next = head1[u];
    head1[u] = edge_cnt1++;
}

void Tarjan(int u)
{
    DFN[u] = LOW[u] = ++dfs_cnt;
    instack[u] = 1;
    Q.push(u);
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if(!DFN[v])
        {
            Tarjan(v);
            if(LOW[u] > LOW[v])
                LOW[u] = LOW[v];
        }
        else if(instack[v] && LOW[u]>DFN[v])
            LOW[u] = DFN[v];
    }
    if(LOW[u] == DFN[u])
    {
        scc_cnt++;
        int j;
        do
        {
            j = Q.top();
            Q.pop();
            instack[j] = 0;
            scc[j] = scc_cnt;
            sp[scc_cnt].num++;
        }
        while(j != u);
    }
    return ;
}

void Toposort()
{
    iq = 0;
    memset(que, 0, sizeof(que));
    for(int i = 1; i <= scc_cnt; i++)
    {
        if(indegree[i] == 0)
            que[iq++] = i;
    }
    for(int i = 0; i < iq; i++)
    {
        for(int k = head1[que[i]]; k != -1; k = edge1[k].next)
        {
            int v = edge1[k].v;
            indegree[v]--;
            if(indegree[v] == 0)
                que[iq++] = v;
        }
    }
}

void solve()
{
    memset(LOW, 0, sizeof(LOW));
    memset(DFN, 0, sizeof(DFN));
    memset(instack, 0, sizeof(instack));
    memset(scc, 0, sizeof(scc));
    memset(sp, 0, sizeof(sp));
    scc_cnt = dfs_cnt = 0;
    while(!Q.empty())
        Q.pop();
    for(int i = 1; i <= n; i++)
        if(!DFN[i])
            Tarjan(i);
    for(int i = 1; i <= scc_cnt; i++)
        for(int j = 1; j <= scc_cnt; j++)
            g[i][j] = (i==j)?0:INF;
    int u, v, w;
    edge_cnt1 = 0;
    memset(edge1, 0, sizeof(edge1));
    memset(head1, -1, sizeof(head1));
    memset(indegree, 0, sizeof(indegree));
    for(int i = 1; i <= n; i++)
    {
        for(int k = head[i]; k != -1; k = edge[k].next)
        {
            int j = edge[k].v;
            if(scc[i] != scc[j])
            {
                u = scc[i];
                v = scc[j];
                w = edge[k].w;
                if(g[u][v] > w)
                    g[u][v] = w;
            }
        }
    }
    for(int i = 1; i <= scc_cnt; i++)
    {
        for(int j = 1; j <= scc_cnt; j++)
        {
            if(g[i][j]!=INF && i!=j)
            {
                add_edge1(i, j, g[i][j]);
                indegree[j]++;
            }
        }
    }
    Toposort();
    while(t--)
    {
        scanf("%d %d", &u, &v);
        if(scc[u] == scc[v])
            printf("0\n");
        else
        {
            u = scc[u];
            v = scc[v];
            for(int i = 1; i <= scc_cnt; i++)
                dis[i] = INF;
            dis[u] = 0;
            for(int i = 0; i < iq; i++)
            {
                for(int k = head1[que[i]]; k != -1; k = edge1[k].next)
                {
                    if(dis[edge1[k].v] > dis[que[i]]+edge1[k].w)
                        dis[edge1[k].v] = dis[que[i]]+edge1[k].w;
                }
            }
            if(dis[v] != INF)
                printf("%d\n", dis[v]);
            else
                printf("Nao e possivel entregar a carta\n");
        }
    }
    printf("\n");
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int u, v, w;
    while(cin>>n>>m)
    {
        if(n <= 0)
            break;
        edge_cnt = 0;
        memset(edge, 0, sizeof(edge));
        memset(head, -1, sizeof(head));
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            add_edge(u, v, w);
        }
        scanf("%d", &t);
        solve();
    }
    return 0;
}
</span>
时间: 2024-10-10 07:26:30

POJ 3114 - Countries in War(强连通分量+缩点+拓扑排序+DAG最短路)的相关文章

POJ 3114 Countries in War 强连通+最短路

用floyd超时了...注定的事情...题意:看案例就跑出来了..不需要看题了把.. #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #include<vector> const int INF =1999299; int minn(int a,int b) { return a>b?b:a; } #define N 510 #define M

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

题目链接 题意是说在几个邮局之间传送一份信件,如果出发点和终止点在同一个国家传递,则时间为0,否则让你求花费最少时间,如果不能传到,则输出Nao e possivel entregar a carta.判断邮局是否在同一个国家的依据是发出的信件可以相互到达. 如果直接求最短路则无法判断两个邮局是否在同一个国家,判断两个邮局是否属于同一个国家的标志是在这个国家邮局间可以相互到达,那么这就是强连通了,所以要先缩点判读邮局是否在同一个国家,如果不是,则重新建图,建图的时候要维护好边权,求出最短边权,在

poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812   Accepted: 4194 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m条有向边,问该图中任意两点x, y之间是否满足x可以到y或者y可以到x. 一开始WA的原因是因为没注意到是或者, 如果是并且的话,就是一道简单的强连通分量的题,直接判断整个图是否为一个强连通分量 对于该题, 先用强连通分量进行缩点,简化图.图就变成了DAG,用拓扑排序判断图中点的入度, 图中入度为0

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

POJ 3114 Countries in War 题目链接 题意:给定一个有向图,强连通分支内传送不需要花费,其他有一定花费,每次询问两点的最小花费 思路:强连通缩点后求最短路即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <stack> #include <algorithm> using namespa

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

题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <vector> 5 #include <stack> 6 #include <queue> 7 #inc

POJ 3114 Countries in War(强连通分量+最短路)

题目大意: n个间谍 他们之间传送信息需要一定的时间一个联通分量里面的间谍属于一个国家,之间的信息传递不需要时间然后问你从一个间谍传一个信息到另一个间谍那需要最少时间 也可能传不到 思路:先缩点,再最短路,由于n最大只有500.可以用邻接矩阵,而且对缩点后的DAG的边权可以做贪心处理,只留两个强连通分量间的最短边长即可. //2852K 297MS C++ 2595B #include<cstdio> #include<iostream> #include<cstring&g

POJ 1236 Network of Schools (强连通分量缩点求度数)

题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他地方到达. 对于问题2, 每个入度为0的scc, 都可以补一条边可以变成强连通图, 每个出度为0的scc, 也可以补一条边使其变成强连通图. 所以答案就是max(入度为0scc个数,出度为0scc个数). #include<cstdio> #include<iostream> #inc

【BZOJ1179】【Apio2009】Atm 强连通分量缩点+拓扑DP/拓扑最长路 kosaraju+tarjan+dfs转非递归三种代码

题解: 首先第一个阶段, 可以写kosaraju.也可以写tarjan. 这两种还都分递归和dfs转非递归. ----------------------------------四种方案. 第二个阶段,可以写拓扑DP 也可以写最长路 ----------------------------------乘上之前的,,八种方案. 本文写了kosaraju递归版,tarjan递归版,kosaraju非递归版. --只怪学校oj系统栈太小..都是逼得啊. 代码1(tarjan): #include <c