程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1135 Domino Effect (spfa + 枚舉)- from lanshui_Yang

POJ 1135 Domino Effect (spfa + 枚舉)- from lanshui_Yang

編輯:C++入門知識

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,具體詳解請看程序:

<span style="font-size:18px;">#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std ;
int n , m ;
const int MAXN = 505 ;
struct Node
{
    int adj ;
    double dis ;
};
const int INF = 0x7fffffff ;
int t ;
vector<Node> 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<int> q ;
bool inq[MAXN] ;
void spfa(int u)  // 求最短路
{
    while (!q.empty())
        q.pop() ;
    q.push(u) ;
    inq[u] = true ;
    d[u] = 0 ;
    int tmp ;
    Node v ;
    while (!q.empty())
    {
        tmp = q.front() ;
        q.pop() ;
        inq[tmp] = false ;
        int i ;
        for(i = 0 ; i < vert[tmp].size() ; i ++)
        {
            v = vert[tmp][i] ;
            if(d[tmp] != INF && d[tmp] + v.dis < d[v.adj])
            {
                d[v.adj] = d[tmp] + v.dis ;
                if(!inq[v.adj])
                {
                    q.push(v.adj) ;
                    inq[v.adj] = true ;
                }
            }
        }
    }
}
void solve()
{
    memset(inq , 0 , sizeof(inq)) ;
    int i , j ;
    for(i = 1 ; i <= n ; i ++)
    {
        d[i] = INF ;
    }
    spfa(1) ;
    double MAX = d[1] ;
    int MAXb = 1 ;
    for(i = 1 ; i <= n ; i ++)  
    {
        if(MAX < d[i])
        {
            MAX = d[i] ;
            MAXb = i ;
        }
    }
    int pan = 0 ;
    int t1 , t2 ;
    for(i = 1 ; i <= n ; i ++) // 枚舉每條邊 , 更新MAX 
    {
        for(j = 0 ; j < vert[i].size() ; j ++)
        {
            Node tn = vert[i][j] ;
            int ta = tn.adj ;
            double td = tn.dis ;
            if((d[i] + d[ta] + td) / 2 > MAX )  // 注意:最大距離的求法
            {
                pan = 1 ;
                MAX = (d[i] + d[ta] + td) / 2;
                if(i < ta)
                {
                    t1 = i ;
                    t2 = ta ;
                }
                else
                {
                    t1 = ta ;
                    t2 = i ;
                }
            }
        }
    }
    printf("The last domino falls after %.1f seconds," , MAX) ;
    if(pan)
    {
        printf(" between key dominoes %d and %d.\n" , t1 , t2) ;
    }
    else
    {
        printf(" at key domino %d.\n" , MAXb) ;
    }
    puts("") ;
}
int ca ;
int main()
{
    ca = 0 ;
    while (scanf("%d%d" , &n , &m) != EOF)
    {
        if(n == 0 && m == 0)
            break ;
        init() ;
        printf("System #%d\n" , ++ ca) ;
        solve() ;
    }
    return 0 ;
}
</span>

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved