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. QQ-weiyun(微云)-云储存

    ylbtech-DatabaseDesgin:QQ-weiyun(微云)-云储存 1.A,数据库关系图(Database Diagram) -- =========================== ...

  2. Shell--变量的显示与设置、环境变量、语系变量

    1.变量的显示与设置:echo,unsetecho:显示一段文字,也可以读出变量内容并打印出来  格式echo $变量或者echo ${变量}语 法:echo [-neE][字符串]或 echo [- ...

  3. 一个基于RSA算法的Java数字签名例子

    原文地址:一个基于RSA算法的Java数字签名例子 一.前言: 网络数据安全包括数据的本身的安全性.数据的完整性(防止篡改).数据来源的不可否认性等要素.对数据采用加密算法加密可以保证数据本身的安全性 ...

  4. linux文件测试操作

    1.文件测试操作 返回 true 如果... -e 文件存在 -a 文件存在 这个选项的效果与-e 相同.但是它已经被弃用了,并且不鼓励使用 -f file 是一个 regular 文件(不是目录或者 ...

  5. How to dynamically load directive into page

    https://stackoverflow.com/questions/23556398/how-to-dynamically-load-directive-into-page I have an h ...

  6. How to check the 'OLE DB Destination' INPUT and OUTPUT

    Step 1: Step 2: Step 3:

  7. java Map根据value排序

    通用方法 public class MapUtil { public static <K, V extends Comparable<? super V>> Map<K, ...

  8. POJ 2375 Cow Ski Area (强连通分量)

    题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅 ...

  9. TCP/IP详解 卷一(第二十章 TCP的成块数据流)

    本章将介绍TCP所使用的被称为滑动窗口协议的一种流量控制方法. 该协议允许发送方在停止并等待确认前可以连续发送多个分组,这样就可以加速数据的传输. 滑动窗口 下图用可视化的方法显示了滑动窗口协议 我们 ...

  10. Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding

    Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding 深度学 ...