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. python 时间 相关

    http://www.jb51.net/article/47957.htm 不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部 ...

  2. pandas的loc, iloc, ix的操作

    参考: https://blog.csdn.net/xw_classmate/article/details/51333646 1. loc——通过行标签索引行数据 2. iloc——通过行号获取行数 ...

  3. CSDN日报20170413 ——《天天写业务代码的那些年,我们是怎样成长过来的》

    [程序人生]天天写业务代码的那些年,我们是怎样成长过来的 作者:Phodal 比起写业务代码更不幸的是,主要工作是修 Bug , bug , buG , bUg. [Java 编程]Springboo ...

  4. 读取Properties文件简易代码

    public class Utils { private static Properties props = new Properties(); static { ClassLoader classL ...

  5. NFS网络文件系统的配置

    NFS网络文件系统的配置 NFS网络文件系统 NFS(network file system)网络文件系统.一种使用于分散式文件协定,有SUN公司开发.功能是通过网络让不同的机器.不同的操作系统能够分 ...

  6. AForge.NET Framework-2.2.5

    http://www.aforgenet.com/framework/downloads.html AForge.NET Framework-2.2.5 简介 AForge.NET是一个专门为开发者和 ...

  7. 小tip: base64:URL背景图片与web页面性能优化

    转自:http://www.zhangxinxu.com/wordpress/?p=2341 一.base64百科 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP ...

  8. requirejs主流程解读

    近期读了下requirejs源代码,那叫一个复杂啊(相对于seajs来说).整理出了逻辑的主要部分的流程图,感兴趣的能够看下.欢迎批评指正~ http://www.gliffy.com/go/publ ...

  9. uva 10034 Freckles (kruskal||prim)

    题目上仅仅给的坐标,没有给出来边的长度,不管是prim算法还是kruskal算法我们都须要知道边的长度来操作. 这道题是浮点数,也没啥大的差别,处理一下就能够了. 有关这两个算法的介绍前面我已经写过了 ...

  10. 解决java.math.BigDecimal divide方法运算结果为无限小数问题

    http://samueli.iteye.com/blog/224755 BigDecimal除法运算报错,错误如下:Non-terminating decimal expansion; no exa ...