BOI'98 DAY 2 TASK 1 CONFERENCE CALL Dijkstra/Dijkstra+priority_queue/SPFA

BOI‘98 DAY 2 TASK 1

CONFERENCE CALL

PROBLEM

A telecom company would like to offer a three-party conference call service. This service enables three customers to participate in a telephone conversation simultaneously. A customer accesses the interconnection network of the telephone system via a switch. The network consists of switches linked by bidirectional lines. Over the existing network, the three participants accessing the network via three different switches, must be connected with the minimum cost. Note that it is possible to connect any two switches. You will be given all the links between the switches of the network along with their associated costs. You are requested to find the connections which will minimize the total cost of interconnecting the three switches to which the participants in a conference call are connected.

INPUT

The input is a text file named conf.inp. The first line contains two numbers : The number of switches, designated by ( N <= 100) and the number of switch-to-switch links in the network, designated by E, separated by a blank. Each of the following E input lines contains three integers, designated by i, j and ci,j where i and j are the numbers of two different switches and ci,jis the cost of using the link between i and j in a conference call (1 <= ci,j<= 100), each separated by one blank. The last line of input contains three integers, which designate the three switches that participate in the conference call, each separated by one blank. The switches are identified with integers 1 through N consecutively.

OUTPUT

The output must be a text file named conf.out. The first line must contain the total (minimal) cost of establishing a conference call among the given three switches and the number R of switch-to-switch connections needed to establish this conference call, separated by a blank. Each of the following R lines must contain two integers, which denote that the link between the switch numbered by the first integer and the switch numbered by the second integer is included in the solution -ordering of between the two switches is not important. The ordering among the last R lines of the output is also not important.

EXAMPLE

分析

这道题可以形象地理解成。班里有45个同学,其中有三个同学想要选某一个同学家作为见面地点,要求这个见面地点到三个同学家的距离和最小。

容易想到运用单源最短路算法,分别以三个客户端作为源点,运行最短路算法。把距离保存在三个数组当中。最后循环每一个点,查哪一个点到三个点的距离和最短。保存这个点t,这个最小费用s。

最后从t开始反向查找会经过的每一条边。

程序

  1 #include<bits/stdc++.h>
  2
  3 using namespace std;
  4
  5 int n, m, EdgeCount, u, v, w, a[3], f[3][105], Head[105], pre[3][105], t, k, s;
  6 struct node
  7 {
  8     int Next, Aim, Weight;
  9 }Edge[20005];
 10 struct h
 11 {
 12     int len,point;
 13     bool operator < (const struct h &x) const
 14     {
 15         return x.len<len;
 16     }
 17     bool operator > (const struct h &x) const
 18     {
 19         return x.len>len;
 20     }
 21 };
 22 struct p
 23 {
 24     int x,y;
 25     bool operator > (const struct p &P) const
 26     {
 27         return (P.x<x)||((P.x==x)&&(P.y<y));
 28     }
 29     bool operator < (const struct p &P) const
 30     {
 31         return (P.x>x)||((P.x==x)&&(P.y>y));
 32     }
 33 }b[20005];
 34
 35 inline void Insert(int u, int v, int w)
 36 {
 37     Edge[++EdgeCount]=(node){Head[u],v,w};
 38     Head[u]=EdgeCount;
 39 }
 40
 41 void Dijkstra(int num,int st)
 42 {
 43     priority_queue<h> Q;
 44     f[num][st]=0;
 45     while(!Q.empty()) Q.pop();
 46     Q.push((struct h){0,st});
 47     for (int j=1;j<=n;j++)
 48     {
 49         struct h x=Q.top();
 50         Q.pop();
 51         for (int i=Head[x.point];i;i=Edge[i].Next)
 52          if (f[num][x.point]+Edge[i].Weight<f[num][Edge[i].Aim])
 53         {
 54             f[num][Edge[i].Aim]=f[num][x.point]+Edge[i].Weight;
 55             Q.push((struct h){f[num][Edge[i].Aim],Edge[i].Aim});
 56             pre[num][Edge[i].Aim]=x.point;
 57         }
 58     }
 59 }
 60
 61 int main()
 62 {
 63     freopen("conference.in","r",stdin);
 64     freopen("conference.out","w",stdout);
 65     cin >> n >> m;
 66     EdgeCount=0;
 67     for (int i = 1; i <= m; i++)
 68     {
 69         cin >> u >> v >> w;
 70         Insert(u, v, w);
 71         Insert(v, u, w);
 72     }
 73     memset(f, 0x3f, sizeof(f));
 74     for (int i = 0; i <= 2; i++)
 75     {
 76         cin>>a[i];
 77         Dijkstra(i,a[i]);
 78     }
 79     s=1e+8;
 80     for (int i = 1; i <= n; i++)
 81         if (s > f[0][i]+f[1][i]+f[2][i])
 82         {
 83             s = f[0][i]+f[1][i]+f[2][i];
 84             t = i;
 85         }
 86     cout << s;
 87     s = 0;
 88     for (int i = 0; i <= 2; i++)
 89     {
 90         k = t;
 91         while (pre[i][k] != 0)
 92         {
 93             b[++s] = (p){min(pre[i][k],k),max(pre[i][k],k)};
 94             k = pre[i][k];
 95         }
 96     }
 97     sort(b+1,b+(s+1));
 98     t = 1;
 99     for (int i = 2; i <= s; i++)
100         if (b[i].x != b[i-1].x || b[i].y != b[i-1].y)
101             t++;
102     cout << " " << t << endl;
103     cout << b[1].x << " " << b[1].y << endl;
104     for (int i=2;i<=s;i++)
105         if (b[i].x != b[i-1].x || b[i].y != b[i-1].y)
106             cout<<b[i].x<<" "<<b[i].y<<endl;
107     return 0;
108 }

BOI'98 DAY 2 TASK 1 CONFERENCE CALL Dijkstra/Dijkstra+priority_queue/SPFA

原文地址:https://www.cnblogs.com/OIerPrime/p/8323706.html

时间: 2024-08-11 11:47:46

BOI'98 DAY 2 TASK 1 CONFERENCE CALL Dijkstra/Dijkstra+priority_queue/SPFA的相关文章

task 限制任务数量(转自msdn)

1 public class LimitedConcurrencyLevelTaskScheduler : TaskScheduler 2 { 3 // Indicates whether the current thread is processing work items. 4 [ThreadStatic] private static bool _currentThreadIsProcessingItems; 5 6 // The maximum concurrency level all

一步一步实现基于Task的Promise库(二)all和any方法的设计和实现

在上一篇中我们已经初步完成了Task类,如果仅仅是这些,那么没有多大意义,因为网上这类js库有很多,现在我们来些更复杂的使用场景. 如果我们现在有这样一个需求:我们要先读取aa.txt的内容,然后去后台解析,同时bb.txt也要读取解析,然后当两个文件都解析完了,我们还要合并两部分内容存到cc.txt中,最后发个通知说ok了..需求很变态,但是我还是想问有没有好的办法呢?按照最原始的嵌套回调的写法好像不是那么容易了,因为你没法知道aa.txt和bb.txt两个文件的读取解析谁先完成,所以你除了要

StackExchange.Redis通用封装类分享

前两天朋友问我,有没有使用过StackExchange.Redis,问我要个封装类,由于之前都是使用ServiceStack.Redis,由于ServiceStack.Redis v4版本后是收费版的,所以现在也很有公司都在使用StackExchange.Redis而抛弃ServiceStack.Redis了.其实个人觉得,两个驱动都不错,只是由于ServiceStack.Redis收费导致目前很多公司都是基于V3版本的使用,也有人说V3版本有很多Bug,没有维护和升级,不过至少目前我是没发现B

Java 网络编程学习总结

新手一枚,Java学习中,把自己学习网络编程的知识总结一下,梳理下知识,方便日后查阅,高手莫进. 本文的主要内容: [1]    网络编程认识                [2]  TCP/IP编程小例子 [3]   UDP编程小例子 [4]  简单线程池实例 一.网络编程的主要认识 Java是一种基于网络设计的语言,用于开发网络软件特别便利. 它对于网络的操作都是基于IP层以上的,也就是对TCP/UDP进行操作,所以java的网络编程又称为Socket编程. 一种是TCP/IP 网络编程,

【开源】OSharp框架解说系列(5.2):EntityFramework数据层实现

〇.前言 上篇 的数据层设计中,我们主要设计了数据对对外开放的 实体基类EntityBase<TKey>,单元操作接口IUnitOfWork 和 数据仓储接口IRepository<TEntity, TKey>,下面我们来解说怎样来使用 EntityFramework 对这些数据访问需求进行实现.EntityFramework 的实现中,我们不仅要实现以上设计的两个接口,还要做以下几件事: 设计一个与 业务实体解耦的 EntityFramework数据上下文类 设计 实体加载方案,

8异步和多线程

1,.NET 中实现异步的方式是使用委托的beginInvoke方法. 使用异步之后.net会创建一个线程来异步执行方法操作,主线程会继续执行程序逻辑.如果在异步执行中有回调函数,在异步方法执行完之后执行异步调用的线程回再执行回调函数中的代码. 1,首先创建一个比较耗时的私有方法用以作为异步执行的代码,后续的多线程执行代码使用的也是该代码. 1 /// <summary> 2 /// 一个比较耗时耗资源的私有方法 3 /// </summary> 4 /// <param n

C# 封装RabbitMQ消息队列处理

现在使用.net领域使用RabbitMQ有很多解决方案,我自己使用过的有两个,一个是EasyNetQ,一个是CAP,都挺好用的,尤其是CAP,懒人推荐使用,怎么使用的文章也很多,小伙伴可以自行搜索. 最近我自己尝试根据目前手头项目的需要,自行封装一下基于RabbitMQ的使用,下面开搞,贴上我自己写的代码. 首先定义消息发布者/生产者接口: 1 using System.Threading.Tasks; 2 3 namespace fx.MQ 4 { 5 public interface IPu

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

关于自动寻径和图、邻接表的学习和启发

以后的游戏中可能会用到人物的自动寻径,在网上看到一个非常不错的博文,特学习了一下,并转了过来为以后留着... 再次感谢 Siliphen的分享,本文转载自 http://blog.csdn.net/stevenkylelee/article/details/38408253 本文乃Siliphen原创,转载请注明出处:http://blog.csdn.net/stevenkylelee 本文的实现使用的环境是:Cocos2d-x 3.2,VS2013 本文,我们最终实现的地图行走效果如下2图: