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. S5PV210使用的启动方式

    2017年12月25日1. S5PV210存储配置: +内置64KB NorFlash(上电不需要初始化)(叫IROM 内部外存):用于存储预先设置的BL0; + SoC内置96KB SRAM(上电不 ...

  2. ES6里关于函数的拓展(三)

    一.箭头函数 在ES6中,箭头函数是其中最有趣的新增特性.顾名思义,箭头函数是一种使用箭头(=>)定义函数的新语法,但是它与传统的JS函数有些许不同,主要集中在以下方面: 1.没有this.su ...

  3. Unity3d / 3ds max 模型分享站点

    http://www.cgrealm.org/model/ 王国3D模型库 http://www.cgjoy.com/ 游戏特效论坛

  4. Android开发Tips(2)

    欢迎Follow我的GitHub, 关注我的CSDN. 我会介绍关于Android的一些有趣的小知识点. 上一篇. 1. Dagger2的开发顺序 Module -> Component -&g ...

  5. Unity3D 中脚本执行的先后顺序

    Unity3D本身自带有控制脚本执行先后顺序的方法: Edit ---> Project Settings ---> Script Execution Order  ---> 值越小 ...

  6. C5:单例模式 Singleton

    保证一个类仅有一个实例,并提供一个访问它的全局访问点. 应用场景:A.一个无状态的类使用单例,可以节省内存B.全局或配置类(其实这个也是无状态的)C.脚本或程序从运行开始到结束,仅需要一个实例来保证数 ...

  7. Codeforces 463C Gargari and Bishops 题解

    题目出处: http://codeforces.com/contest/463/problem/C 感觉本题还是挺难的.须要好好总结一下. 计算对角线的公式: 1 右斜对角线,也叫主对角线的下标计算公 ...

  8. C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用 &amp;&amp; 并查集方法)

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. AlamoFireDemo

    // // ViewController.swift // AlamFireDemo // // import UIKit import Alamofire class ViewController: ...

  10. TCP应用程序通信协议的处理

    TCP应用程序通信协议的处理 flyfish 2015-6-29 一 流式处理 TCP是一种流协议(stream protocol).TCP数据是以字节流的形式传递给接收者的,没有固有的"报 ...