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.
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.
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;
}