设为首页 加入收藏

TOP

POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang(一)
2014-11-23 21:38:14 】 浏览:3444
Tags:POJ 1135 Domino Effect spfa 枚举 from lanshui_Yang
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.
题目大意:给你n个关键的多米诺骨牌,这n个关键的多米诺骨牌由m条由骨牌组成的“路”相连,每条路都有自己的“长度”,当这n个骨牌中的任意一个骨牌 k 倒塌时,与k相连的所有“路”上的骨牌也会随之而倒,让你求把骨牌 1 推到后,所有骨牌中最后一个倒塌的骨牌距离骨牌1的最短距离。
解题思路:题目中保证图是连通的,我们可以先求出骨牌1到其他(n - 1)个关键骨牌的最短距离,得到这些距离中的最大值MAX,然后枚举图中的每条边,再更新MAX,具体详解请看程序:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std ;
int n , m ;
const int MAXN = 505 ;
struct Node
{
    int adj ;
    double dis ;
};
const int INF = 0x7fffffff ;
int t ;
vector vert[MAXN] ;
double d[MAXN] ;  // 保存顶点 1 到其他(n - 1)个顶点的最短距离
void clr()  // 初始化
{
    int i ;
    for(i = 0 ; i < MAXN ; i ++)
        vert[i].clear() ;
    memset(d , 0 ,sizeof(d)) ;
}
void init()
{
    clr() ;
    int i , j ;
    Node tmp ;
    for(i = 0 ; i < m ; i ++) // 用邻接表建图
    {
        int a , b ;
        double c ;
        scanf("%d%d%lf" , &a , &b , &c) ;

        tmp.adj = b ;
        tmp.dis = c ;
        vert[a].push_back(tmp) ;

        tmp.adj = a ;
        tmp.dis = c ;
        vert[b].push_back(tmp) ;
    }
}
queue q ;
bool inq[MAXN] ;
void spfa(int u)  // 求最短路
{
    while (!q.empty())
        q.pop() ;
    q.push(u) ;
    inq[u
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇标准C++中的string类的用法总结 下一篇CF 319B Psychos in a Line

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目