[POJ] 1135 Domino Effect
Domino Effect
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12147 Accepted: 3046
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
Southwestern European Regional Contest 1996
到每个点的最短路就是推到这个点的最短时间
处理边就用(dis[i]+dis[j]+w)/2更新最大值
然后比较最大值和每个dis里的最大值,大于说明就是在边上推到了结束,否则就是在这个最大值的点上结束的。
特判n=1的点。。。
输出 真的 恶心
为什么 .1lf 就WA .1f 就AC
迷
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN=505;
const int INF=1<<28;
int n,m;
struct Edge{
int next,to,w;
}e[MAXN*MAXN];
int ecnt,head[MAXN];
inline void add(int x,int y,int w){
e[++ecnt].next = head[x];
e[ecnt].to = y;
e[ecnt].w = w;
head[x] = ecnt;
}
bool vis[MAXN];
int dis[MAXN];
int baned[MAXN];
struct Node{
int id,w;
bool operator<(const Node &rhs) const{
return w>rhs.w;
}
};
void dij(){
priority_queue<Node> Q;
vis[1]=0;
for(int i=2;i<=n;i++)
dis[i]=INF,vis[i]=0;
for(int i=1;i<=n;i++)
Q.push(Node{i,dis[i]});
for(int i=1;i<=n;i++){
Node top;
do{
top=Q.top() ;
Q.pop();
}while(vis[top.id]);
int mnid=top.id,mn=top.w;
vis[mnid]=1;
for(int i=head[mnid];i;i=e[i].next){
int v=e[i].to ;
if(dis[v]>mn+e[i].w){
dis[v]=mn+e[i].w ;
// baned[v]=mnid;
Q.push(Node{v,dis[v]});
}
}
}
}
int main(){
int cnt=1;
while(cin>>n>>m){
// memset(baned,0,sizeof(baned));
if(!n) return 0;
ecnt=0;
for(int i=1;i<=n;i++) head[i]=0;
for(int i=1;i<=m;i++){
int x,y,w;
cin>>x>>y>>w;
add(x,y,w);
add(y,x,w);
}
dij();
double mx=0.0;
double single_mx=-INF;
int mxid;
for(int i=1;i<=n;i++) {
if(single_mx<dis[i]){
single_mx=dis[i];
mxid=i;
}
}
int l=0,r=0;
for(int i=1;i<=n;i++){
bool flag=0;
if(dis[i]==INF) continue;
for(int j=head[i];j;j=e[j].next){
int v=e[j].to ;
// if(i==baned[v]) continue;
if(dis[v]==INF) continue;
flag=1;
if((1.0*(dis[i]+dis[v]+e[j].w)/2.0)>mx){
mx=(double)(1.0*(dis[i]+dis[v]+e[j].w)/2.0);
l=v<i?v:i;
r=v>=i?v:i;
}
}
}
printf("System #%d\n",cnt++);
if(n==1){
printf("The last domino falls after 0.0 seconds, at key domino 1.\n\n");//
continue;
}
if(mx<=single_mx){
// The last domino falls after 27.0 seconds, at key domino 2.
printf("The last domino falls after %.1f seconds, at key domino %d.\n",mx,mxid);
}else{
// The last domino falls after 7.5 seconds, between key dominoes 2 and 3.
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n"
,mx,l,r);
}
printf("\n");
}
}
[POJ] 1135 Domino Effect的更多相关文章
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
- POJ 1135 Domino Effect (Dijkstra 最短路)
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9335 Accepted: 2325 Des ...
- POJ 1135.Domino Effect Dijkastra算法
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10325 Accepted: 2560 De ...
- POJ 1135 Domino Effect(Dijkstra)
点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...
- POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- CF 405B Domino Effect(想法题)
题目链接: 传送门 Domino Effect time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
- zoj 1298 Domino Effect (最短路径)
Domino Effect Time Limit: 2 Seconds Memory Limit: 65536 KB Did you know that you can use domino ...
随机推荐
- ReenTrantLock可重入锁和synchronized的区别
ReenTrantLock可重入锁和synchronized的区别 可重入性: 从名字上理解,ReenTrantLock的字面意思就是再进入的锁,其实synchronized关键字所使用的锁也是可重入 ...
- CentOS 部署RabbitMQ集群
1. 准备两台CentOS,信息如下: node1:10.0.0.123 node2:10.0.0.124 修改hostname请参照: $ hostname # 查看当前的hostname $ ho ...
- MySQL慢查询日志的使用
当系统性能达到瓶颈的时候,就需要去查找那些操作对系统的性能影响比较大,这里可以使用数据库的慢查询日志功能来记录一些比较耗时的数据可操作来确定哪些地方需要优化. 下面介绍一下使用慢查询日志的一些常用命令 ...
- 详解JS作用域(一)
一.什么是作用域 存储和访问变量,是任何一种编程语言最基本的功能之一,变量存在哪里?程序需要时如何找到它?这些问题需要一套良好的规则来规范,这套规则,就成为作用域. 二.编译原理 js通常归类为解释语 ...
- Linux 增加虚拟内存
1. 用 df -h 命令找一个比较大的磁盘空间 2.建立swap文件 大小为2G count= 3.启用虚拟内存 1. 将swap文件设置为swap分区文件 mkswap swapfile #(由于 ...
- JavaScript 获取浏览器版本
//获取IE版本function GetIEVersions(){ var iejson={ isIE:false,safariVersion:0 }; var ua = navigator.user ...
- [牛客网试题] Test.main() 函数执行后的输出是()
public class Test { public static void main(String [] args){ System.out.println(new B().getValue()); ...
- 数据库查询,显示为树形结构(easyui+SSM)
在实际项目上,有很多地方后台存了一个表,但是在显示查询的时候需要显示为树形结构. 本项目是easyui+SSM框架. 前台程序为: <!DOCTYPE html> <html> ...
- 2013上半年中国CRM市场分析报告
经过了十多年的风风雨雨,CRM度过了漫长的市场培育期,即将迎来成熟期.目前这一阶段也是CRM惨烈搏杀的一个阶段,据不完全统计,国内大大小小的CRM厂商已经超过600家,各厂商几度火拼,努力扩大自己在C ...
- CF1062D Fun with Integers
思路: 找规律. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ...