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 Sample Output System #1 Source |
题目意思是每组有n个关键多米诺骨牌和m行骨牌,每行骨牌全部倒下要t秒(这里可以把关键骨牌理解成结点,把行理解成连接两个结点的带权边),然后从第一个结点推倒多米诺骨牌,问最后停在哪里(结点或边上)和全部倒下花费的时间。显然这题是单源最短路问题,首先用Dijkstra算出从结点1开始到其它所有点的时间d[],接下来分为2种情况:(1):最后一个骨牌倒的在结点,那么答案就是max{d[]},停在的点就是max{d[]}对应的结点;(2):停在边上,就暴力所有的边,求所有停在边xy上的时间(d[x]+d[y]+w[x][y])/2的最大值就是最后要花费的时间,具体代码如下:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#define MAX 600
#define INF 0x7FFFFFFF
# define ePS 1e-
using namespace std;
int Min(int x , int y){
if(x<y)return x;
return y;
}
int n,m;
int w[MAX][MAX],v[MAX],d[MAX];//边的信息(w[i][j]表示i->j的距离,INF表示不通),标记,最短距离存放 void dijkstra(int u0)//源点为u0的Single-Source Shortest Paths
{
memset(v,,sizeof(v)); //清除所有点的标号
for(int i=;i<=n;i++)d[i]=(i==u0 ? :INF);//设d[u0]=0,其它d[i]=INF;
for(int i=;i<=n;i++){//循环n次
int x,min=INF;
for(int y=;y<=n;y++)if(!v[y] && d[y]<=min)min=d[x=y];//1在所有未标号的节点中,选出d值最小的节点x
v[x]=;//2给出节点x标记//3对于从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w(x,y)}
for(int y=;y<=n;y++)if(!v[y] && w[x][y]<INF)d[y]=Min(d[y],d[x]+w[x][y]);
}
}
//----------------------------------------------------------------------------------------
int main(){
int tt = ;
while(cin>>n>>m){
if(n==&&m==)break;
for(int i=;i<=n;i++){//初始化
for(int j=;j<=n;j++){
if(i==j)w[i][j]=;
else w[i][j]=INF;
}
}
for(int i=;i<m;i++){//构图edge[a][b]=c:a->b时间为c
int a,b,c;
cin>>a>>b>>c;
w[a][b] = c;
w[b][a] = c;
}
dijkstra();//求出1到所有定点最短路 double max1 = -;//情况a停在某个结点上,That is the longest road of d[]
int index;
for(int i=;i<=n;i++){
if(max1<d[i]){
max1=d[i]*1.0;
index=i;
}
} double max2 = -;
int index1,index2;
for(int i=; i<=n; i++){//情况b停在普通的牌上(即:边上的普通多米诺骨牌)
for(int j=; j<=n; j++){//暴力枚举所有边求最大值
if(w[i][j]!=INF && i<j){//i<j优化作用
if(max2<(d[i]+d[j]+w[i][j])/2.0){
max2 = (d[i]+d[j]+w[i][j])/2.0;
index1 = i;
index2 = j;
}
}
}
} printf("System #%d\n",tt++);
if(max1 >= max2)//选出符合的情况
printf("The last domino falls after %.1f seconds, at key domino %d.\n",max1,index);
else
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",max2,index1,index2);
printf("\n");
}
return ;
}
/*
^_^:Dijkstra算法(正权图上的单源最短路 Single-Source Shortest Paths)
即从单个节点出发,到所有节点的最短路径,该算法适合于有向图和无向图)
:清除所有点的标号
设d[0]=0,其它d[i]=INF;
循环n次
{
在所有未标号的节点中,选出d值最小的节点x
给出节点x标记
对于从x出发的所有边(x,y),更新d[y]=min{d[y],d[x]+w(x,y)}
}
:假设起点是结点0,它到结点i的路径长度为d[i],v[i]=0未标号,w[i][j]=INF路径不存在
memset(v,0,sizeof(v));
for(int i=0;i<n;i++)d[i]=(i==0 ? 0:INF);
for(int i=0;i<n;i++){
int x,min=INF;
for(int y=0;y<n;y++)if(!v[y] && d[y]<=min)min=d[x=y];
v[x]=1;
for(int y=0;y<n;y++)d[y]=min(d[y],d[x]+w[x][y]);//INF取适当大,防止越界!!!或加一个判断是否xy连通
}
:除了求出最短路的长度外,也能很方便地打印所有结点0到所有结点最短路本身(从终点出发
,不断顺着d[i]+w[i][j]==d[j]的边(i,j)从结点j"退回"到结点i,直到回到起点。此外,也
可以在更新d时维护"父亲指针".具体来说就是把d[y]=min(....)改成:
if(d[y]>d[x]+w[x][y]){
d[y]=d[x]+w[x][y];
fa[y]=x;
}
*/
[ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)的更多相关文章
-
【算法】单源最短路——Dijkstra
对于固定起点的最短路算法,我们称之为单源最短路算法.单源最短路算法很多,最常见的就是dijkstra算法. dijkstra主要用的是一种贪心的思想,就是说如果i...s...t...j是最短路,那么 ...
-
用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
-
Dijkstra算法——单源最短路算法
一.介绍 迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他各个节点的最短路径. 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止. 适用于有 ...
-
图论算法(二)最短路算法:Floyd算法!
最短路算法(一) 最短路算法有三种形态:Floyd算法,Shortset Path Fast Algorithm(SPFA)算法,Dijkstra算法. 我个人打算分三次把这三个算法介绍完. (毕竟写 ...
-
Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32824 Accepted: 11098 Description Bes ...
-
最短路模板(Dijkstra &; Dijkstra算法+堆优化 &; bellman_ford &; 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
-
单源最短路——dijkstra算法
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 问 ...
-
【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home
https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...
-
模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
随机推荐
-
自动生成数据库字典(sql2008)
每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 存储过程: SET ANSI_NULLS ON GO SET QUOT ...
-
IIS性能相关的配置、命令
IIS性能相关的配置.命令 应用程序池回收 不要使用缺省的“固定时间间隔(分钟)”:1740(即29小时),建议改为0 可以根据实际情况设置特定时间回收,比如凌晨4点 最大工作进程数 可以根据实际情况 ...
-
转:如何在32位程序中突破地址空间4G的限制
//如何在32位程序中突破地址空间4G的限制 //首先要获得内存中锁定页的权限 #define _WIN32_WINNT 0x0501 //xp系统 #include <windows.h> ...
-
Matlab中给figure添加图例(legend),标题(title)和颜色(color)
在Matlab绘图过程中,尤其是需要将多个图绘制在相同的坐标轴中时,通常需要将不同的曲线设置成为不同的颜色.此外,为了直观,还需要给这张图标增添标题和图例.这篇文章展示了在Matlab的绘图窗口(fi ...
-
c# 操作excel 替代方案
一直使用excel com 接口进行excel 操作,最近一次因为权限折腾了个够呛,果断放弃,使用 NPOI FileStream file = new FileStream(url, FileMod ...
-
uitableview的重用重叠问题
以前也遇到过.但都不知道怎么就解决了. 今天费了一番功夫找到了最佳解决方案. 对于一些复杂的cell 从来都是用自定义的方法,但是如果复杂的cell里面内容多了.特别是图片加载,那难免会出现重叠重用 ...
-
关于for循环中的闭包问题
还是昨天的那个简单的小项目,已经花了一天的时间了 - - .从&&的用法,到CSStext,到今天马上要谈的闭包(closure),通过一个小东西,真真发现了自己的各方面不足.昨天发完 ...
-
Transform 位置 旋转
using UnityEngine; using System.Collections; using Box2D.Dynamics; public class BodyGameObj : MonoBe ...
-
[Hadoop] - Protocol Buffer安装
Hadoop从2.x版本开始,底层的RPC远程调用使用ProtocolBuffer格式来传递数据,所以在编译Hadoop的过程中有可能出现提示缺少Protocol服务的异常信息,类似:'protoc ...
-
github下载项目代码到本地,不能运行 本地改完代码 再上传
首先用git bash here,在指定目录下执行, git clone 将项目拉取下来, 试运行: 发现需要配置idea的SDK/jdk, 还要选择language level, 建立输出目录tar ...