poj 1135 Domino Effect

Domino Effect
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12325 Accepted: 3083

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

Southwestern European Regional Contest 1996

思路:最后倒下的牌有两种情形

一  、最后倒下的是关键牌,其时间和位置就是第一张关键牌到其他关键牌的最短路径的最大值 记作max1

二、最后倒下的是普通牌,其时间和位置就是两张关键牌之间的某张普通牌,其时间为max2=(d[i]+d[j]+G[i][j])/2.0  G[i][j]表示的就是两张相邻关键牌之间倒下所需要的时间   

如果max2>max1 就是第二种情况  否则就是第一种情况

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=505;
const double INF=10000000;
struct edge{
    int to;
    int cost;
}e;
int caseno=1;
typedef pair<int,int> P;//first 表示最短距离  second表示顶点的编号
int n,m;
vector<edge> G[maxn];
double d[maxn];
void dijkstra(int s)
    {
        priority_queue<P,vector<P>,greater<P> > que;
        fill(d,d+n+1,INF);//fill 要改成d+n+1才行  因为从1开始计数
        d[s]=0.0;
        que.push(P(0.0,s));
        while(!que.empty())
            {
                P p=que.top();
                que.pop();
                int v=p.second;
                if(d[v]<p.first) continue;
                for (int i=0;i<G[v].size();i++)
                    {
                        edge e=G[v][i];
                        if(d[e.to]>d[v]+e.cost)
                            {
                                d[e.to]=d[v]+e.cost;
                                que.push(P(d[e.to],e.to));
                            }
                    }
            }
    }

int main()
    {
        int a,b,c;
        while(cin>>n>>m)
            {
                if(n==0&&m==0) break;
                double maxn1=-1,maxn2=-1;
                for(int i=1;i<=n;i++) G[i].clear();
                while(m--)
                    {
                        scanf("%d",&a);
                        scanf("%d%d",&b,&c);
                        e.to=b;
                        e.cost=c;
                        G[a].push_back(e);
                        e.to=a;
                        G[b].push_back(e);//重点强调  这里wa了我一个小时  无向图所以要进行两次赋值
                    }
                dijkstra(1);
                int m1=0,m2=0,m3=0;
                for(int i=2;i<=n;i++)
                    {
                        if(maxn1<d[i])
                        {
                            maxn1=d[i];
                            m1=i;
                        }
                        //cout<<d[i]<<endl;
                    }
                //cout<<maxn1<<endl;
                for (int i=0;i<=n;i++)
                    {
                        for(int j=0;j<G[i].size();j++)//从i到j的之间的距离
                            {
                                e=G[i][j];
                                double temp=(e.cost+d[e.to]+d[i])/2.0;
                                if(maxn2<temp)
                                {
                                    maxn2=temp;
                                    m2=i;
                                    m3=e.to;
                                }
                            }
                    }
                if(n==1) {maxn1=0.0;m1=1;}
                if(maxn2>maxn1)
                {
                    if(m2>m3) swap(m2,m3);
                    printf("System #%d\nThe last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",caseno++,maxn2,m2,m3);
                }
                else
                    printf("System #%d\nThe last domino falls after %.1f seconds, at key domino %d.\n\n",caseno++,maxn1,m1);
            }
            return 0;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值