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 ...
随机推荐
- CF919D Substring
思路: 拓扑排序过程中dp.若图有环,返回-1. 实现: #include <bits/stdc++.h> using namespace std; ; vector<int> ...
- MVC中使用MVCPager简单分页
一.建立数据库以及建立MVC项目 自己随便建立一个数据库,并且添加数据.我建立的数据库如下. 二.建立LINQ to SQL映射. 然后一步步点确定 三.编写代码 在Controllers中建立控制器 ...
- vim插件minibuf配置
1.去下载网站下载minibufexpl.vim文件放入到~/vim/plugins中,有的系统路径是~/.vim/plugins; 下载网址如下 https://www.vim.org/script ...
- iOS programming Code Snippet Library
iOS programming Code Snippet Library The freebie code comes from the code snippet library. 代码来自cod ...
- 富文本KindEditor使用
1.官网down KindEditor,添加到自己的项目中:添加时可把不需要的文件夹干掉,asp/php等等.我的项目用的是纯html和js,直接调用后台api: 2.页面引入相关js.eclipse ...
- python与shell通过微信企业号发送消息
python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...
- OpenFlow_tutorial_4_Create_a_Learning_Switch
一.环境搭建: 教程里提供的VM image需要梯子才能下载,好不容易下载下来,发现镜像很难用,各种安装问题,搞了好几天也解决不了.后来就自己搭环境,主要是安装Ryu. 1.首先下载相应的python ...
- chsh - 改变登录 shell
总览 (SYNOPSIS) chsh [ -s shell ] [ -l ] [ -u ] [ -v ] [ username ] 描述 (DESCRIPTION) chsh 用于 改变 用户的 登录 ...
- java图片放大或缩小
package org.jimmy.autotranslate20181022.utils; import java.awt.Graphics; import java.awt.image.Buffe ...
- 获取汉字首字母,拼音,可实现拼音字母搜索----npm js-pinyin
npm install js-pinyin main.js 引入 import pinyin from 'js-pinyin' 使用组件内 let pinyin = require('js- ...