poj 3114 Countries in War

Countries in War
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3675 Accepted: 1060

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

Source

South America 2006, Brazil Subregion

提示

题意:

2050年,第三次世界大战爆发,其中有n个城市(1<=n<=500),每个城市都有一个间谍,这些间谍需要向其他间谍发送信息,下面将给出e(0<=e<=n*n)条城市间的传递信息消耗时间,如果城市同属于一个国家,传递信息不耗时,当且仅当A城市与B城市能相互传递信息表示它们属于同一个国家,求出对于k(0<=k<=100)个询问城市间传递信息所需要消耗最少的时间,如果不能传递则输出"Nao e possivel entregar a carta"。

思路:

缩点,建图,跑一次最短路就行。

下面给出邻接矩阵做法。

示例程序

Source Code

Problem: 3114		Code Length: 2848B
Memory: 2372K		Time: 282MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
#define MAX 1e9+7
int map[2][500][500],dfn[500],low[500],vis[500],stack[20000],id[500],count,deep,top;
void init(int n,int flag)			//flag表示当前是旧图还是新图
{
    int i,i1;
    for(i=0;n>i;i++)
    {
        for(i1=0;n>i1;i1++)
        {
            map[flag][i][i1]=MAX;
        }
        map[flag][i][i]=0;
    }
}
void tarjan(int n,int t)
{
    int pos,i;
    dfn[t]=deep;
    low[t]=deep;
    deep++;
    vis[t]=1;
    stack[top]=t;
    top++;
    for(i=0;n>i;i++)
    {
        if(map[0][t][i]!=MAX)
        {
            if(dfn[i]==-1)
            {
                tarjan(n,i);
                if(low[t]>low[i])
                {
                    low[t]=low[i];
                }
            }
            else if(vis[i]==1&&low[t]>dfn[i])
            {
                low[t]=dfn[i];
            }
        }
    }
    if(dfn[t]==low[t])
    {
        do
        {
            top--;
            pos=stack[top];
            id[pos]=count;
            vis[pos]=0;
        }while(t!=pos);
        count++;
    }
}
void bulid(int n)
{
    int i,i1;
    init(count,1);				//新图初始化
    for(i=0;n>i;i++)
    {
        for(i1=0;n>i1;i1++)
        {
            if(id[i]!=id[i1]&&map[1][id[i]][id[i1]]>map[0][i][i1])	//对边贪心,也就是取最小的那一个
            {
                map[1][id[i]][id[i1]]=map[0][i][i1];
            }
        }
    }
}
void spfa(int s,int t)
{
    int v[500],d[500],q[20000],i,f=0,top=0;
    for(i=0;count>i;i++)
    {
        v[i]=0;
        d[i]=MAX;
    }
    v[s]=1;
    d[s]=0;
    q[top]=s;
    top++;
    while(f<top)
    {
        v[q[f]]=0;
        for(i=0;count>i;i++)
        {
            if(d[i]>d[q[f]]+map[1][q[f]][i])
            {
                d[i]=d[q[f]]+map[1][q[f]][i];
                if(v[i]==0)
                {
                    q[top]=i;
                    top++;
                    v[i]=1;
                }
            }
        }
        f++;
    }
    if(d[t]==MAX)
    {
        printf("Nao e possivel entregar a carta\n");
    }
    else
    {
        printf("%d\n",d[t]);
    }
}
int main()
{
    int t,i,i1,n,m,q,u,v,w;
    scanf("%d %d",&n,&m);
    while(n!=0)
    {
        memset(dfn,-1,sizeof(dfn));
        memset(low,-1,sizeof(low));
        memset(vis,0,sizeof(vis));
        count=0;
        deep=0;
        top=0;
        init(n,0);					//图的初始化
        for(i=1;m>=i;i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            if(map[0][u-1][v-1]>w)				//防止重边
            {
                map[0][u-1][v-1]=w;
            }
        }
        for(i=0;n>i;i++)
        {
            if(dfn[i]==-1)
            {
                tarjan(n,i);
            }
        }
        bulid(n);						//新图建立
        scanf("%d",&q);
        for(i=1;q>=i;i++)
        {
            scanf("%d %d",&u,&v);
            spfa(id[u-1],id[v-1]);
        }
        printf("\n");
        scanf("%d %d",&n,&m);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值