Domino Effect
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10454   Accepted: 2590

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

题解:

这是一道最短路径题目,但是要判断哪种最优解的情况。设res为最后一张牌倒下的时刻,p1p2记录的是最后倒下的关键牌的序号1  

a) 如果最后一张倒下的是关键牌。利用Dijkstra 算法求第张关键牌到其他每张关键牌的最短路径,保存在dis[i]。然后取dis[i]的最大值,设为resp0..记录关键牌序号。

b) 如果最后一张倒下的是两张关键牌之间的普通牌。设该行两端的关键牌为i j,他们以每秒一个单位的速度相向而行,设ij分别经过t1t2秒相遇,ij之间的距离为map[i][j],ij在什么时刻相遇。不难列出方程:t1+t2=map[i][j]dis[i]+map[i][j]=dis[j]+map[i][j]。则ij相遇的时刻为t=dis[i] + dis[j] +map[i][j]/2.0res=minrest)。p1p2记录两张关键牌的序号。(注意:要满足dis[i]+map[i][j]>dis[j]并且dis[j]+map[i][j]>dis[i]才应该计算t值) 

c) 如果gard==1,则是a情况;否则是b情况(如果b情况成立,pos_j的值应该改变了)。

每一次AC背后都是无数次WA

对比两次的读入(只有读入不同)

WA代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<climits>
#include<cstdlib>
using namespace std;
#define N 501
#define inf INT_MAX
int n,m,tot,map[N][N],vis[N],dis[N];
int main(){
while(scanf("%d%d",&n,&m)==&&n&&m){
memset(map,,sizeof map);
memset(vis,,sizeof vis);
memset(dis,,sizeof dis);
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
map[u][v]=map[v][u]=w;
}
vis[]=;
for(int i=;i<=n;i++){
dis[i]=(map[][i]!=?map[][i]:inf);
}
int t=;
dis[]=;//dijkstra
for(int i=;i<n;i++){
int mi=inf;t=;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<mi){
t=j;
mi=dis[j];
}
}
vis[t]=;
for(int j=;j<=n;j++){
if(!vis[j]&&map[t][j]!=&&dis[j]>dis[t]+map[t][j]){
dis[j]=dis[t]+map[t][j];
}
}
}
int p0=,p1=,p2=,gard=;//开始找关键牌
double res=-inf;
for(int i=;i<=n;i++){
if(res<dis[i]){
res=dis[i];
p0=i;
}
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
double x1=1.0*(dis[i]+dis[j]+map[i][j])/2.0;
if(map[i][j]&&x1>res){
gard=;
res=x1;
p1=i;
p2=j;
}
}
}
printf("System #%d\n", ++tot);
if(gard==){
printf("The last domino falls after %.1lf seconds, at key domino %d.\n", res, p0);
}
else{
if(p1>p2) swap(p1,p2);
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n", res, p1, p2);
}
putchar('\n');
}
return ;
}

AC代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<climits>
#include<cstdlib>
using namespace std;
#define N 501
#define inf INT_MAX
int n,m,tot,map[N][N],vis[N],dis[N];
int main(){
scanf("%d%d",&n,&m);
for(;n+m;){
memset(map,,sizeof map);
memset(vis,,sizeof vis);
memset(dis,,sizeof dis);
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
map[u][v]=map[v][u]=w;
}
vis[]=;
for(int i=;i<=n;i++){
dis[i]=(map[][i]!=?map[][i]:inf);
}
int t=;
dis[]=;//dijkstra
for(int i=;i<n;i++){
int mi=inf;t=;
for(int j=;j<=n;j++){
if(!vis[j]&&dis[j]<mi){
t=j;
mi=dis[j];
}
}
vis[t]=;
for(int j=;j<=n;j++){
if(!vis[j]&&map[t][j]!=&&dis[j]>dis[t]+map[t][j]){
dis[j]=dis[t]+map[t][j];
}
}
}
int p0=,p1=,p2=,gard=;//开始找关键牌
double res=-inf;
for(int i=;i<=n;i++){
if(res<dis[i]){
res=dis[i];
p0=i;
}
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
double x1=1.0*(dis[i]+dis[j]+map[i][j])/2.0;
if(map[i][j]&&x1>res){
gard=;
res=x1;
p1=i;
p2=j;
}
}
}
printf("System #%d\n", ++tot);
if(gard==){
printf("The last domino falls after %.1lf seconds, at key domino %d.\n", res, p0);
}
else{
if(p1>p2) swap(p1,p2);
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n", res, p1, p2);
}
putchar('\n');
scanf("%d%d",&n,&m);
}
return ;
}

我醉了~~tyts

poj1135的更多相关文章

  1. POJ-1135 Domino Effect---最短路Dijk

    题目链接: https://vjudge.net/problem/POJ-1135 题目大意: 有N个关键的多米诺骨牌,这些牌通过一些路径相连接,这些路径是由一排其他骨牌构成的.已知每一条路径上的骨牌 ...

  2. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  3. POJ1135 Domino Effect(SPFA)

    题目大概是,普通骨牌连接两张关键骨牌,一旦一张关键骨牌倒下与其相邻的普通骨牌也倒下,普通骨牌倒下与其相邻的骨牌也倒下.给出所有有普通骨牌相连的两个关键骨牌之间普通骨牌倒下所需时间,问1号关键骨牌开始倒 ...

  4. POJ1135 Domino Effect

    题目:http://poj.org/problem?id=1135 只是求以1为起点的最短路罢了.稍稍判断一下在边上的情况. 多亏提醒:毒数据——n==1!一定要dis [ k ] >= ans ...

  5. POJ1135比较有意思的对短路(多米骨牌)

    题意:      有一个骨牌游戏,就是推到一个后所有的牌都会被退到的那种游戏,起点是1,有两种骨牌,一种是关键牌,另一种是普通牌,普通牌是连接关键牌用的,给你一些边a b c的意思是关键牌a倒之后c时 ...

  6. ACM/ICPC 之 最短路径-dijkstra范例(ZOJ2750-POJ1135(ZOJ1298))

    最短路经典算法-dijkstra范例(两道),第一道是裸的dijkstra,第二道需要枚举所有边已找到可能的情况. ZOJ2750-Idiomatic Phrases Game 题意:见Code 题解 ...

  7. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  8. 2017年暑假ACM集训日志

    20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...

随机推荐

  1. Solr6.6.0 用 SimplePostTool索引文件

    一.背景介绍 Solr启动并运行之后,并不包含任何数据,在solr的安装目录下的bin目录中,有一个post工具,我们可以使用这个工具往solr上传数据,这个工具必须在命令行中执行,post工具是一个 ...

  2. 机器学习算法实践:朴素贝叶斯 (Naive Bayes)(转载)

    前言 上一篇<机器学习算法实践:决策树 (Decision Tree)>总结了决策树的实现,本文中我将一步步实现一个朴素贝叶斯分类器,并采用SMS垃圾短信语料库中的数据进行模型训练,对垃圾 ...

  3. SQL注入的几种有用办法

    一.查询表中包括有多少列: 这里以DISCUZ举例说明,例如以下 select * FROM pre_forum_thread ORDER BY 80 返回,Unknown column '80' i ...

  4. winrm.cmd

    C:\Windows\system32>winrm.cmdWindows 远程管理命令行工具 Windows 远程管理(WinRM)是 WS-Management 协议的 Microsoft 实 ...

  5. P6 EPPM 16 R1安装和配置文档

    白桃花心木P6企业项目组合管理文档库  描述 链接 下载 零件号  16 R1用户和集成文档 查看库 下载 E68199-01 16 R1安装和配置文档 查看库 下载 E68198-01 描述 链接 ...

  6. foreach 加& 什么意思?

    foreach 加&遍历的同时改变原数组即修改数据或者增加数据 foreach 加& 什么意思? 注意:如果我要改变数组某一个值 直接遍历的话原数组是不会变的 下面提供两种方法 1.我 ...

  7. Cocos2d-X中创建菜单项

    Cocos2d-X中创建菜单的类: CCMenuItemFont:创建纯文本的菜单项 CCMenuItemAtlasFont:创建带有艺术字体的菜单项 CCMenuItemImage:用图片创建菜单项 ...

  8. 【Excle数据透视表】如何快速选定数据透视表的汇总行并添加绿色底纹

    数据透视表创建好之后,如何批量将汇总行的底色修改为绿色呢?目标效果图如下: 解决方案 "启用选定内容"选取所有汇总行 单击任意汇总字段(如:北京 汇总)→选择→启用选定内容→开始→ ...

  9. 【Datasatge】使用Datastage装载数据时候,报错:Missing record delimiter “”,saw EOF instead

    如题,报错截图如下: 根据以上警告信息我们可以清晰看出,是字段DEFAULT_FLAG出错了!于是我们找到对应的字段,结果一看,导出文件中DS表结构中该字段为DECIMAL(18,2),但是导出文件中 ...

  10. asp.net中UpdatePanel数据加载成功后回调

    //添加UpdatePanel加载成功后执行的js方法 Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(onPageLoade ...