开发者社区> 史迪奇2号> 正文

[ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

简介:
+关注继续查看


Description

Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing them on end with only a small distance in between. If you do it right, you can tip the first domino and cause all others to fall down in succession (this is where the phrase ``domino effect'' comes from). 
While this is somewhat pointless with only a few dominoes, some people went to the opposite extreme in the early Eighties. Using millions of dominoes of different colors and materials to fill whole halls with elaborate patterns of falling dominoes, they created (short-lived) pieces of art. In these constructions, usually not only one but several rows of dominoes were falling at the same time. As you can imagine, timing is an essential factor here. 
It is now your task to write a program that, given such a system of rows formed by dominoes, computes when and where the last domino falls. The system consists of several ``key dominoes'' connected by rows of simple dominoes. When a key domino falls, all rows connected to the domino will also start falling (except for the ones that have already fallen). When the falling rows reach other key dominoes that have not fallen yet, these other key dominoes will fall as well and set off the rows connected to them. Domino rows may start collapsing at either end. It is even possible that a row is collapsing on both ends, in which case the last domino falling in that row is somewhere between its key dominoes. You can assume that rows fall at a uniform rate.

Input

The input file contains descriptions of several domino systems. The first line of each description contains two integers: the number n of key dominoes (1 <= n < 500) and the number m of rows between them. The key dominoes are numbered from 1 to n. There is at most one row between any pair of key dominoes and the domino graph is connected, i.e. there is at least one way to get from a domino to any other domino by following a series of domino rows. 
The following m lines each contain three integers a, b, and l, stating that there is a row between key dominoes a and b that takes l seconds to fall down from end to end. 
Each system is started by tipping over key domino number 1. 
The file ends with an empty system (with n = m = 0), which should not be processed.

Output

For each case output a line stating the number of the case ('System #1', 'System #2', etc.). Then output a line containing the time when the last domino falls, exact to one digit to the right of the decimal point, and the location of the last domino falling, which is either at a key domino or between two key dominoes(in this case, output the two numbers in ascending order). Adhere to the format shown in the output sample. The test data will ensure there is only one solution. Output a blank line after each system.

Sample Input

2 1
1 2 27
3 3
1 2 5
1 3 5
2 3 5
0 0

Sample Output

System #1
The last domino falls after 27.0 seconds, at key domino 2.

System #2
The last domino falls after 7.5 seconds, between key dominoes 2 and 3.

Source

 

题目意思是每组有n个关键多米诺骨牌和m行骨牌,每行骨牌全部倒下要t秒(这里可以把关键骨牌理解成结点,把行理解成连接两个结点的带权边),然后从第一个结点推倒多米诺骨牌,问最后停在哪里(结点或边上)和全部倒下花费的时间。显然这题是单源最短路问题,首先用Dijkstra算出从结点1开始到其它所有点的时间d[],接下来分为2种情况:(1):最后一个骨牌倒的在结点,那么答案就是max{d[]},停在的点就是max{d[]}对应的结点;(2):停在边上,就暴力所有的边,求所有停在边xy上的时间(d[x]+d[y]+w[x][y])/2的最大值就是最后要花费的时间,具体代码如下:

 

复制代码
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <string>
  8 #define MAX 600
  9 #define INF 0x7FFFFFFF
 10 # define ePS 1e-5
 11 using namespace std;
 12 int Min(int x , int y){
 13     if(x<y)return x;
 14     return y;
 15 }
 16 int n,m;
 17 int w[MAX][MAX],v[MAX],d[MAX];//边的信息(w[i][j]表示i->j的距离,INF表示不通),标记,最短距离存放
 18 
 19 void dijkstra(int u0)//源点为u0的Single-Source Shortest Paths 
 20 {
 21     memset(v,0,sizeof(v));   //清除所有点的标号
 22     for(int i=1;i<=n;i++)d[i]=(i==u0 ? 0:INF);//设d[u0]=0,其它d[i]=INF;
 23     for(int i=1;i<=n;i++){//循环n次
 24       int x,min=INF;
 25       for(int y=1;y<=n;y++)if(!v[y] && d[y]<=min)min=d[x=y];//1在所有未标号的节点中,选出d值最小的节点x
 26       v[x]=1;//2给出节点x标记//3对于从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w(x,y)}
 27       for(int y=1;y<=n;y++)if(!v[y] && w[x][y]<INF)d[y]=Min(d[y],d[x]+w[x][y]);
 28     } 
 29 }
 30 //----------------------------------------------------------------------------------------
 31 int main(){
 32     int tt = 1;
 33     while(cin>>n>>m){
 34         if(n==0&&m==0)break;
 35         for(int i=1;i<=n;i++){//初始化
 36             for(int j=1;j<=n;j++){
 37                 if(i==j)w[i][j]=0;
 38                 else w[i][j]=INF;
 39             }
 40         }
 41         for(int i=0;i<m;i++){//构图edge[a][b]=c:a->b时间为c
 42             int a,b,c;
 43             cin>>a>>b>>c;
 44             w[a][b] = c;
 45             w[b][a] = c;
 46         }
 47         dijkstra(1);//求出1到所有定点最短路
 48         
 49         double max1 = -10000000;//情况a停在某个结点上,That is the longest road of d[]
 50         int index;
 51         for(int i=1;i<=n;i++){
 52             if(max1<d[i]){
 53                 max1=d[i]*1.0;
 54                 index=i;
 55             }
 56         }
 57         
 58         double max2 = -10000000;
 59         int index1,index2;
 60         for(int i=1; i<=n; i++){//情况b停在普通的牌上(即:边上的普通多米诺骨牌)
 61             for(int j=1; j<=n; j++){//暴力枚举所有边求最大值
 62                 if(w[i][j]!=INF && i<j){//i<j优化作用
 63                     if(max2<(d[i]+d[j]+w[i][j])/2.0){
 64                         max2 = (d[i]+d[j]+w[i][j])/2.0;
 65                         index1 = i;
 66                         index2 = j;
 67                     }
 68                 }
 69             }
 70         }
 71             
 72         printf("System #%d\n",tt++);
 73         if(max1 >= max2)//选出符合的情况
 74             printf("The last domino falls after %.1f seconds, at key domino %d.\n",max1,index);
 75         else
 76             printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",max2,index1,index2);
 77         printf("\n");
 78     }
 79     return 0;
 80 }
 81 /*
 82 ^_^:Dijkstra算法(正权图上的单源最短路 Single-Source Shortest Paths)
 83     即从单个节点出发,到所有节点的最短路径,该算法适合于有向图和无向图)
 84    :清除所有点的标号
 85     设d[0]=0,其它d[i]=INF;
 86     循环n次
 87     {
 88         在所有未标号的节点中,选出d值最小的节点x
 89         给出节点x标记
 90         对于从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w(x,y)}
 91     }
 92    :假设起点是结点0,它到结点i的路径长度为d[i],v[i]=0未标号,w[i][j]=INF路径不存在
 93     memset(v,0,sizeof(v));
 94     for(int i=0;i<n;i++)d[i]=(i==0 ? 0:INF);
 95     for(int i=0;i<n;i++){
 96       int x,min=INF;
 97       for(int y=0;y<n;y++)if(!v[y] && d[y]<=min)min=d[x=y];
 98       v[x]=1;
 99       for(int y=0;y<n;y++)d[y]=min(d[y],d[x]+w[x][y]);//INF取适当大,防止越界!!!或加一个判断是否xy连通
100     }
101    :除了求出最短路的长度外,也能很方便地打印所有结点0到所有结点最短路本身(从终点出发
102     ,不断顺着d[i]+w[i][j]==d[j]的边(i,j)从结点j"退回"到结点i,直到回到起点。此外,也
103     可以在更新d时维护"父亲指针".具体来说就是把d[y]=min(....)改成:
104     if(d[y]>d[x]+w[x][y]){
105       d[y]=d[x]+w[x][y];
106       fa[y]=x;
107     }
108 */
复制代码



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【小Y学算法】每日LeetCode打卡——15.实现 strStr()
📢前言 🌲原题样例 🌻C#方法一:暴力法 🌻C#方法二:傻瓜解法 🌻Java 方法一:暴力法 🌻Java 方法二: KMP 解法 💬总结
21 0
算法模板:数据结构之单调队列
算法模板:数据结构之单调队列
10 0
NOIP-C++大神培养计划Step1.1.1基础算法——模拟算法1
模拟算法,可以说是最基础的算法了。它的基本定义没太多意思:就是去模拟题目的要求。题意要你怎么做,你就怎么做,看懂了题目,基本上就会做了。 举一个大家耳熟能详的栗子。 A+B Problem给定两个整数A和B,输出他们的和。
1326 0
NOIP-C++大神培养计划 Step1.1.2基础算法——模拟算法2
大家好,我是小笨笨,今天我们继续来讲解模拟算法。 我们直接上例题! 栗1.1.2-1 洛谷P1014 Cantor表https://www.luogu.org/problemnew/show/P1014题目描述现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。
1072 0
算法模板:模拟数据结构之并查集
算法模板:模拟数据结构之并查集
13 0
深夜学算法之SkipList:让链表飞
1. 前言 上次写Python操作LevelDB时提到过,有机会要实现下SkipList。摘录下wiki介绍: 跳跃列表是一种随机化数据结构,基于并联的链表,其效率可比拟二叉查找树。
763 0
算法学习之路|用C++刷算法会用到的STL(二)——set
用C++刷算法会用到的STL(二)——set
2457 0
一步一步深入理解Dijkstra算法
先简单介绍一下最短路径: 最短路径是啥?就是一个带边值的图中从某一个顶点到另外一个顶点的最短路径。 官方定义:对于内网图而言,最短路径是指两顶点之间经过的边上权值之和最小的路径。 并且我们称路径上的第一个顶点为源点,最后一个顶点为终点。
2110 0
POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm.
984 0
862
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
OceanBase 入门到实战教程
立即下载
阿里云图数据库GDB,加速开启“图智”未来.ppt
立即下载
实时数仓Hologres技术实战一本通2.0版(下)
立即下载