题意:

多米诺骨牌效应:若干个关键牌相连,关键牌之间含有普通牌,关键牌倒下后其所在的行的普通牌全部倒下。求从推倒1号关键牌开始,最终倒下的牌的位置及时间。

分析:

最终倒下的牌的位置有两种情况,要么是正好为关键牌,要么是在两个关键牌之间的普通牌。前者可以利用最短路求出每个关键牌倒下的时间,而各行普通牌全部倒下的时间为该行两个关键牌倒下时间与该行从一端倒向另一端的时间和的一半,即 (t[i]+t[j]+e[i][j])/2,选出两种情况的最大值进行比较,两者中较大值即为多米诺骨牌完全倒下的时间。

代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
#define rep(i,a,n) for(int i =(a); i < (n); i++)
#define sa(n) scanf("%d",&(n))
int n;
const int maxn=550;
const int INF=0x3fffffff;
int t[maxn],s[maxn],e[maxn][maxn];
void dijkstra()
{
int u;
rep(i,0,n){
t[i]=e[0][i];s[i]=0;
}
t[0]=0;
rep(i,0,n-1){
int mins = INF;
rep(j,0,n){
if(!s[j]&&t[j]<mins){
u=j;
mins=t[j];
}
}
s[u]=1;
rep(k,0,n){
if(!s[k]&&e[u][k]<INF)
t[k]=min(t[k],e[u][k]+t[u]);
}
}
}
int main(void)
{
int m,c=1;
int a,b,l;
sa(n);sa(m);
while(n!=0||m!=0){
fill(t,t+maxn,INF);
rep(i,0,n) rep(j,0,n) e[i][j]=INF;
rep(i,0,m){
sa(a);sa(b);sa(l);
e[a-1][b-1]=e[b-1][a-1]=l;
} dijkstra(); double maxtime=0;
int p=1;
rep(i,0,n){
if(maxtime<t[i]){
maxtime=t[i];
p=i+1;
}
}
double time,emax=0;
int p1,p2;
rep(i,0,n){
rep(j,0,n){
if(e[i][j]<INF){
time=(t[i]+t[j]+e[i][j])/2.0;
if(time>emax){
emax=time;
p1=i+1;p2=j+1;
}
}
}
}
printf("System #%d\n",c++);
if(maxtime<emax)
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",emax,p1,p2);
else
printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",maxtime,p);
sa(n);sa(m);
}
return 0;
}

ZOJ 1298_Domino Effect的更多相关文章

  1. zoj 1298 Domino Effect (最短路径)

    Domino Effect Time Limit: 2 Seconds      Memory Limit: 65536 KB Did you know that you can use domino ...

  2. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  3. ZOJ 3777-Problem Arrangement(状压DP)

    B - Problem Arrangement Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  4. GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1)

    GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1) 前言: 无意打开GooglePlay app来着,然后发现首页用了揭示效果,连起来用着感觉还不错. 不清楚 ...

  5. Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果

    Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果 前言: 每次写之前都会来一段(废)话.{心塞...} Google Play首页两个tab背景 ...

  6. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  7. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  8. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  9. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

随机推荐

  1. Android 实现对多个EditText的监听

    create_account=(EditText)findViewById(R.id.create_account); create_password=(EditText)findViewById(R ...

  2. 【PostgreSQL-9.6.3】Red Hat 4.4.7下的安装

    1. 下载源码包https://www.postgresql.org/ftp/source/v9.6.1/ 2. 上传到/opt目录下 3. 创建postgres用户及dba组,并修改压缩包的属主属组 ...

  3. iOS 对overflow:scroll使用

    让子标签的高度在初始化的时候就比父标签大,可以设置height: 101%:这样就出发了内置的scrollview的滚动. -webkit-overflow-scrolling:touch;可以让滚动 ...

  4. 套接字、UDP通信、TCP通信、TCP/IP协议簇

    一.套接字(socket) 1.英语单词socket:n.插座:穴:v.插入插座 2.套接字就是源IP地址和目的IP地址.源端口号和目的端口号的组合,是通过传输层进行通信的.IP指定电脑,端口指定某一 ...

  5. OpenFlow_tutorial_3_Learn_Development_Tools

    一.Several Utilities OpenFlow Tutorial VM 中预装了一些OpenFlow特性的工具和一般通用网络的工具. 1.Openflow Controller:处于Open ...

  6. CREATE AGGREGATE - 定义一个新的聚集函数

    SYNOPSIS CREATE AGGREGATE name ( BASETYPE = input_data_type, SFUNC = sfunc, STYPE = state_data_type ...

  7. C#中练级orcle数据查询

    直接贴代码哈哈哈, public DataTable getInfo(int flag) { OracleConnection conn = null; DataSet ds = new DataSe ...

  8. JavaSE-06 二维数组

    学习要点 二维数组的定义 二维数组内存数据结构 不规则二维数组 二维数组的定义 语法格式 格式一 数据类型[][] 数组名 = new 数据类型[m][n]; m:表示这个二维数组有多少个一维数组. ...

  9. hdfs深入:07、hdfs的文件的读取过程

    详细步骤解析 1. Client向NameNode发起RPC请求,来确定请求文件block所在的位置: 2. NameNode会视情况返回文件的部分或者全部block列表,对于每个block,Name ...

  10. 关于idea的目录结构如何变成树状,也就是横向变纵向

    横向 竖向 方法: