您的位置:首页 > Web前端

Poj 1135 Domino Effect【很有意识的最短路+SPFA】

2016-06-16 17:47 423 查看
Domino Effect
Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 10415

 

Accepted: 2581

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

 

题目大意:有一堆多米诺骨牌,有n个关键点,有m条路,对于m条路,输入a,b,c表示从a推到b需要耗时间c。问从1号关键点开始推将所有多米诺骨牌都推倒需要耗费多长时间。

思路:

1、首先,求一遍单源最短路,其源点当然要设为1。

2、遍历一遍dis【i】,找到最长路设定为maxn,记录对应节点。

3、遍历所有边,在这条边中有这样三条信息:dis【u】,dis【v】,w【u,v】。我们知道,分别代表:从节点1最快速度推到节点u的时间,从节点1最快速度推到节点v的时间,以及u,v之间的时间,其表达如图:



那么我们不难理解这样两个结论,

①如果有这样的边存在,那么终点就不能能是一个节点,而是一条边中的某个地方。

②如果有这样的边存在,那么完成这一片区域的推倒时间为:(dis【u】+dis【v】+w【u,v】)/2 。

4、那么我们就来枚举每一条边,如果有这样的边存在了,那么就计算一次(dis【u】+dis【v】+w【u,v】)/2 。维护其最大值记做maxn2,并且记录这条边的两个节点。这时候如果maxn2>maxn。那么就输出其maxn2,和最大值对应的两个节点。相反的,输出maxn和那个单独节点。

注意的点:

1、G++提交需要使用f来输出,C++提交需要lf来输出。

2、注意输出格式。

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int head[100000];
struct EdgeNode
{
int to;
int w;
int next;
}e[100000];
int vis[10000];
int dis[10000];
int n,m,cont;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA(int ss)
{
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
memset(vis,0,sizeof(vis));
queue<int >s;
s.push(ss);
dis[ss]=0;
vis[ss]=1;
while(!s.empty())
{
int u=s.front();
s.pop();
vis[u]=0;
for(int j=head[u];j!=-1;j=e[j].next)
{
int w=e[j].w;
int v=e[j].to;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
int pos=1;
double maxn=0;
for(int i=2;i<=n;i++)
{
if(dis[i]>maxn)
{
pos=i;
maxn=dis[i];
}
}
int pa,pb;
double maxn2=0;
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=e[j].next)
{
int v=e[j].to;
int w=e[j].w;
double tmp=(dis[i]+dis[v]+w)/2.0;
if(tmp>maxn2)
{
maxn2=tmp;
pa=i;
pb=v;
}
}
}
if(maxn2>maxn)
{
printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",maxn2,pa,pb);
}
else printf("The last domino falls after %.1f seconds, at key domino %d.\n",maxn,pos);
}
int main()
{
int kase=0;
while(~scanf("%d%d",&n,&m))
{
cont=0;
memset(head,-1,sizeof(head));
if(n==0&&m==0)break;
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
printf("System #%d\n",++kase);
SPFA(1);
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Poj 1135 pku 1135