ZOJ 3396 Conference Call


Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Good News! Utopia Polycom begins to offer a whole new service, Conference Call. This new service enables three users to make phone calls at the same time and talk to each other. It's very useful when several people are discussing about one shared topic. For example, Tom, Rosemary and you plan to go picnic tomorrow. You just need to make one conference call, so that you can discuss with Tom and Rosemary simultaneously. It's extremely convenient, yes?

However, it has a lot of technological problems to start this service. It took engineers of Utopia Polycom five years to conquer all these problems. One of the biggest problem is how to make the communication line connected. To solve this problem, they have constructed a powerful net of transfer stations. Every single phone is connected to its nearest transfer station, some transfer stations are connected to each other by wires. When to make a conference call, signal will start from the dialling telphone. The signal will be transferred through transfer stations and finally reach the other two phones.

Since the distances between each two transfer stations are various, the cost for the signal to go through the wires is different as well. Assume there are n telphones(numbered from 1 to n) and m(numbered from1 to m) transfer stations in this communication net. The total cost for a line is the sum of the costs of the wires in the line. Our problems remains, what is the minimal total cost to make a conference call among three telphone ab and c. As showing below, the minimal total cost to make a conference call among telphone 123 is 12.

Input

There are multiple cases. For each test case, the first line contain three positive integers, n (n ≤ 10000), m (m ≤ 500), l (l ≤ 10000). l means the number of wires. Then follows n lines. Each line contains one integer ti (1 ≤ ti ≤ m), which means the number of transfer station No.i telphone is connected to. Then follows l lines, each line contains three integers abC (1 ≤ C ≤ 500), which means a and b transfer stations are connected to each other by a wire with the amount of cost C. If a pair (ab) doesn't appear in these l lines, that means tranfer stations ab are not connected.

The next line is a single integer q (q ≤ 50). Then follow q lines. Each line contains three integers xyz. You are supposed to calculate the minimal total cost to make a conference call among telphone xyz. Each test case is seperated by a blank line. Proceed to the end of the file.

Output

For each test case i, print case number in the form "Case #i" in one single line. Then for each inquiry j, print one line in the form "Line j: " and if the conference call can be made and the minimal total cost isMin, print the sentense "The minimum cost for this line is Min." else you should print "Impossible to connect!". Look at the samples for details.

Sample Input

3 5 5
2
1
5
1 2 6
2 3 1
3 4 2
4 5 3
1 5 12
1
1 2 3

3 3 1
1
2
3
2 3 4
1
1 2 3

Sample Output

Case #1
Line 1: The minimum cost for this line is 12.
Case #2
Line 1: Impossible to connect!

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int INF=0x3f3f3f3f;

int N,M,L,Q,telephone[11000],G[600][600],dist[600][600],vis[600];
bool use[600];

void dijkstra(int src)
{
    dist[src][src]=0;
    memset(use,false,sizeof(use));
    for(int i=0;i<M;i++)
    {
        int mark=-1,dd=INF;
        for(int j=1;j<=M;j++)
        {
            if(use[j]) continue;
            if(dist[src][j] < dd)
            {
                dd=dist[src][j]; mark=j;
            }
        }

        if(mark==-1) return;

        use[mark]=true;
        for(int j=1;j<=M;j++)
        {
            if(use[j]) continue;
            if(dist[src][j]>dist[src][mark]+G[mark][j])
            {
                dist[src][j]=dist[src][mark]+G[mark][j];
            }
        }
    }
}

int main()
{
     int cas=1;
while(scanf("%d%d%d",&N,&M,&L)!=EOF)
{
    printf("Case #%d\n",cas++);
    for(int i=1;i<=N;i++)   scanf("%d",telephone+i);
    memset(G,63,sizeof(G));
    memset(dist,63,sizeof(dist));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<L;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        G[a][b]=G[b][a]=c;
    }
    scanf("%d",&Q);

    for(int j=1;j<=Q;j++)
    {
        printf("Line %d: ",j);
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        x=telephone[x];
        y=telephone[y];
        z=telephone[z];

        if(!vis[x]) vis[x]=1,dijkstra(x);
        if(!vis[y]) vis[y]=1,dijkstra(y);
        if(!vis[z]) vis[z]=1,dijkstra(z);

        int ans=INF;
        for(int i=1;i<=M;i++)
        {
            ans=min(ans,dist[x][i]+dist[y][i]+dist[z][i]);
        }

        if(ans==INF) printf("Impossible to connect!\n");
        else printf("The minimum cost for this line is %d.\n",ans);
    }
}
    return 0;
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值