poj1135 dijkstra
来源:互联网 发布:淘宝安装工怎样 编辑:程序博客网 时间:2024/06/05 03:25
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.
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#define maxn 0x7fffff
using namespace std;
int n,m;
int dist[600];
int map[600][600];
int mark[600];
void dijkstra()
{
int i,j,min1,minj,temp;
dist[1]=0; //把起点并入集合,搜索时就可以从起点寻找到第一条最短的边了
for (i=1;i<=n-1;i++)
{
min1=maxn;
for (j=1;j<=n;j++) //查找到原集合的最短的边
{
if (!mark[j]&&dist[j]<min1)
{
min1=dist[j];
minj=j;
}
}
mark[minj]=1;
for (j=1;j<=n;j++) //每并入一个点都要对原来的边进行修正,保证任意时刻源点到目标点的距离都是最短的。
{
if (!mark[j]&&map[minj][j]>0)
{
temp=dist[minj]+map[minj][j];
if (temp<dist[j])
dist[j]=temp;
}
}
}
}
int main()
{
int t;
t=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=maxn;
for (int i=1;i<=n;i++) dist[i]=maxn;
memset(mark,0,sizeof(mark));
for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
dijkstra();
double ans1=-maxn;
double ans2=-maxn;
int qi;
int wi,wj;
for(int i=1;i<=n;i++)
{
if(ans1<dist[i])
{
ans1=dist[i];
qi=i;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(map[i][j]<maxn&&ans2<(dist[i]+dist[j]+map[i][j])*1.0/2)
{
ans2=(dist[i]+dist[j]+map[i][j])*1.0/2;
wi=i;
wj=j;
}
}
printf("System #%d\n",t++);
if(ans1>=ans2)
printf("The last domino falls after %.1lf seconds, at key domino %d.\n",ans1,qi);
else
printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",ans2,wi,wj);
cout<<endl;
}
}
- poj1135 dijkstra
- POJ1135 DIJKSTRA+枚举。
- poj1135最短路Dijkstra算法
- ZOJ1298 POJ1135 Domino Effect,Dijkstra 算法
- POJ1135&&ZOJ1298--Domino Effect【dijkstra+枚举】
- POJ1135
- poj1135 Domino Effect
- poj1135 - Domino Effect
- ZOJ1298, POJ1135 Domino Effect
- POJ1135(最短路)
- POJ1135 Domino Effect(最短路)
- POJ1135 多米诺效应(单源最短路径+枚举)
- dijkstra
- dijkstra
- Dijkstra
- Dijkstra
- DIJKSTRA
- DIJKSTRA
- 对QSqlite数据库的操作
- 函数重载和虚函数
- 写文件时最好使用绝对路径, 而不使用相对路径
- Countif的用法
- 第一章
- poj1135 dijkstra
- 我的评论有“-1”条
- linux 安装配置DNS服务器
- CASE: MS SQL SERVER 2008 R2 service could not be started.
- 因为老看到prototype,转下js的prototype用法
- Cookie与会话--Session与SSO的实现细节研究
- 4,2011.9.1用户登录,更换,密码更改
- 8/31/2011 10:51:45 AM
- 数据泵导入SKIP_CONSTRAINT_ERRORS的bug(三