ZOJ 1298_Domino Effect
题意:
多米诺骨牌效应:若干个关键牌相连,关键牌之间含有普通牌,关键牌倒下后其所在的行的普通牌全部倒下。求从推倒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的更多相关文章
- zoj 1298 Domino Effect (最短路径)
Domino Effect Time Limit: 2 Seconds Memory Limit: 65536 KB Did you know that you can use domino ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- ZOJ 3777-Problem Arrangement(状压DP)
B - Problem Arrangement Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1)
GooglePlay 首页效果----tab的揭示效果(Reveal Effect) (1) 前言: 无意打开GooglePlay app来着,然后发现首页用了揭示效果,连起来用着感觉还不错. 不清楚 ...
- Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果
Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果 前言: 每次写之前都会来一段(废)话.{心塞...} Google Play首页两个tab背景 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
随机推荐
- java之java.lang.UnsupportedClassVersionError:com/mysql/jdbc/Driver : Unsupported major.minor version 52.0
问题解释:jdk版本和mysql驱动版本不兼容,比如:jdk1.7与mysql-connector-java-5.xxx兼容,但与mysql-connector-java-6.xxx及以上不兼容
- skeljs框架关键点使用
global 全局 style.css containers: 1400px;容器宽度 xlarge 超大屏(media: max-width:1680px) style-xlarge.cs ...
- 纯CSS写的对勾样式
& .cicle{ position: relative; float: right; margin-right: -1rem; ...
- Android开发中使用代码删除数据库
更多信息参考:Android开发中使用代码删除数据库 在Android开发中,如果用到数据库,就会有一个很麻烦的问题,就是有时候需要删除数据库很麻烦,要打开Android Device Monitor ...
- LitePal用法详解
一.首先我对数据库的操作基于LitePal的,是基于面向对象思想的,所以首先我先讲怎么使用LitePal 1.在build.garde(Module:app)里面的 dependencies{ //添 ...
- sql把两值之和当作条件进行查询
目的:把表中两个字段之和作为where条件进行过滤查询 //查询在没有过期的记录select a,b from test where a+b>now();// a:存入时间 b:有效期时间段 进 ...
- 手动将Excel数据导入SQL
1. 右键点击数据库名称,Tasks --> Import Data... 2. Choose Data Source:MicroSoft Excel,选择Excel文件和Excel版本.Exc ...
- Spring工作原理及其作用
1.springmvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作. 2.DispatcherServlet查询一个或多个Hand ...
- ALTER FUNCTION - 修改一个函数的定义
SYNOPSIS ALTER FUNCTION name ( [ type [, ...] ] ) RENAME TO newname DESCRIPTION 描述 ALTER FUNCTION 修改 ...
- JS Object 属性判断
in 方法 var shapeInfo = {name:“lium”}; if (“name” in shapeInfo) {...}