poj1135
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10454 | Accepted: 2590 |
Description
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 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
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为最后一张牌倒下的时刻,p1和p2记录的是最后倒下的关键牌的序号1。
a) 如果最后一张倒下的是关键牌。利用Dijkstra 算法求第1 张关键牌到其他每张关键牌的最短路径,保存在dis[i]。然后取dis[i]的最大值,设为res,p0..记录关键牌序号。
b) 如果最后一张倒下的是两张关键牌之间的普通牌。设该行两端的关键牌为i 和j,他们以每秒一个单位的速度相向而行,设i和j分别经过t1和t2秒相遇,i和j之间的距离为map[i][j],求i和j在什么时刻相遇。不难列出方程:t1+t2=map[i][j],dis[i]+map[i][j]=dis[j]+map[i][j]。则i和j相遇的时刻为t=(dis[i] + dis[j] +map[i][j])/2.0。res=min(res,t)。p1和p2记录两张关键牌的序号。(注意:要满足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的更多相关文章
- POJ-1135 Domino Effect---最短路Dijk
题目链接: https://vjudge.net/problem/POJ-1135 题目大意: 有N个关键的多米诺骨牌,这些牌通过一些路径相连接,这些路径是由一排其他骨牌构成的.已知每一条路径上的骨牌 ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- POJ1135 Domino Effect(SPFA)
题目大概是,普通骨牌连接两张关键骨牌,一旦一张关键骨牌倒下与其相邻的普通骨牌也倒下,普通骨牌倒下与其相邻的骨牌也倒下.给出所有有普通骨牌相连的两个关键骨牌之间普通骨牌倒下所需时间,问1号关键骨牌开始倒 ...
- POJ1135 Domino Effect
题目:http://poj.org/problem?id=1135 只是求以1为起点的最短路罢了.稍稍判断一下在边上的情况. 多亏提醒:毒数据——n==1!一定要dis [ k ] >= ans ...
- POJ1135比较有意思的对短路(多米骨牌)
题意: 有一个骨牌游戏,就是推到一个后所有的牌都会被退到的那种游戏,起点是1,有两种骨牌,一种是关键牌,另一种是普通牌,普通牌是连接关键牌用的,给你一些边a b c的意思是关键牌a倒之后c时 ...
- ACM/ICPC 之 最短路径-dijkstra范例(ZOJ2750-POJ1135(ZOJ1298))
最短路经典算法-dijkstra范例(两道),第一道是裸的dijkstra,第二道需要枚举所有边已找到可能的情况. ZOJ2750-Idiomatic Phrases Game 题意:见Code 题解 ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- 2017年暑假ACM集训日志
20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...
随机推荐
- 深入理解ES6里的promise
一.ES6 Promise是什么? 复杂的概念先不讲,我们先简单粗暴地把Promise用一下,有个直观感受.那么第一个问题来了,Promise是什么呢?是一个类?对象?数组?函数? 别猜了,直接打印出 ...
- ElasticSearch 相关性
1.相关性 ElasticSearch检索结果是按照相关性倒序排列的,相关性是什么,相关性又是如何计算的?每个文档都有相关性评分,用一个正浮点数字段 _score 来表示 . _score 的评分越高 ...
- 【Consul】 分布式环境中的服务注册和发现利器
参考资料: http://www.cnblogs.com/shanyou/p/4695131.html http://blog.csdn.net/viewcode/article/details/45 ...
- Java模式的秘密--java常用的几种模式
要学习设计模式,首先要明白设计模式,就是为实现某一种或某一组功能提供的代码编码方式.它没有固定的套路,只有约定俗成的风格.所有编码者可以根据已有的设计模式开放思维,设计出自己的设计模式,也会在无意中使 ...
- lodash 数组元素查找 findIndex
_.findIndex(array, [predicate=_.identity]) 这个方法类似 _.find.除了它返回最先通过 predicate 判断为真值的元素的 index ,而不是元素本 ...
- vue-router push
//push 方法一 对象 // this.$router.push({path:'HelloWorld2'}); //push 方法二 命名的路由 // this.$router.push({nam ...
- HDU1789 Doing Homework again 【贪心】
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- xml中处理特殊字符和转义字符
XML 中的特殊字符 > 和 开始标记 > 例如: 5 ]] 如何获得这些HTML内容呢? XmlDocument doc = new XmlDocument(); doc.Load(&q ...
- 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)
1.如图所看到的我们须要在前台的页面通过radio和select将相应的数据库中的数据显示到选项其中,这也是我们做项目中常常须要做的,动态的显示,而不是静态的显示. 首先我们须要在页面中导入strut ...
- Centos7 搭建最新 Nexus3 Maven 私服
Maven 介绍 Apache Maven 是一个创新的软件项目管理和综合工具.Maven 提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文 ...