CUGB图论专场:A - Domino Effect(最短路径:dijkstra算法)

来源:互联网 发布:青春旅社网络更新时间 编辑:程序博客网 时间:2024/05/21 10:16

A - Domino Effect
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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 11 2 273 31 2 51 3 52 3 50 0

Sample Output

System #1The last domino falls after 27.0 seconds, at key domino 2.System #2The last domino falls after 7.5 seconds, between key dominoes 2 and 3.

这题搞了好久,算是又重新学了最短路径,以前没有理解好吧,所以现在又把各个算法敲了一遍,又对比了一下,收获良多……

刚开始是想让时间效率快点嘛!所以想都没有想就用了spfa,无语WA了3发,实在不行了,就又看了PPT一遍,才发现dijkstra算法可以求最优解。想spfa错了,还以为是dijkstra的优化版本呢,原来是bellman_ford的优化版本,唉……第一题就搞了这么久,不过终于把最短路径的这几个算法弄清楚了,以为学了没有拿几题练,后悔啊,以后学算法不能这样了!!!

此题题意我也理解了好久才明白是什么意思!!!英语实在很烂啊……

题目大意:

  有n个多米诺骨牌,有m条边,推倒第1张牌,以这个点为端点边上的的牌同时倒,问最后倒下的那张牌是哪张,并且求出时间。如果正好是端点上的牌,输出端点序号,否则需要输出这个点在哪两个端点之间。

先求端点的情况,然后再求两个端点之间的情况,比较最大就是哪种情况……

#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define M 505#define INF 1000000using namespace std;typedef long long ll;int t,n,c=1,Map[M][M];void init(){    int s, e, tmp,i,j;    for (i = 0; i < M; i++)    {        for (j = 0; j < M; j++)            Map[i][j] = INF;        Map[i][i] = 0;    }    for (i = 0; i < t; i++)    {        scanf("%d%d%d", &s, &e,&tmp);        Map[s][e] = tmp;        Map[e][s] = tmp;  //如果路径不是按从小到大排的刚要加这句    }}void dijkstra(int s){    int i,j,k,dist[M],visited[M],index,index1,index2,min;    double max1=-M,max2=-M;    mem(visited,0);    for (i = 1; i <= n; i++)        dist[i] = Map[s][i];    visited[s] = 1;    for (j = 1; j <= n - 1; j++)    {        min = INF;        for (k = -1, i = 1; i <= n; i++)            if (!visited[i] && dist[i] < min)            {                k = i;                min = dist[i];            }        if (k != -1)        {            visited[k] = 1;            for ( i = 1; i <= n; i++)            {                if (!visited[i] && dist[k] + Map[k][i] < dist[i] && dist[k] != INF && Map[k][i] != INF)                    dist[i] = dist[k] + Map[k][i];            }        }    }    //下面是判断最优情况    for(i=1; i<=n; i++)        if(max1 < dist[i])        {            max1 = dist[i];            index = i;        }    for(i=1; i<=n; i++)        for(j=1; j<=n; j++)        {            if(Map[i][j] != INF && i < j) //i<j因为输出都是从小到大排列的            {                if(max2 < (dist[i]+dist[j]+Map[i][j])/2.0)                {                    max2 = (dist[i]+dist[j]+Map[i][j])/2.0;                    index1 = i;                    index2 = j;                }            }        }    if(max1>=max2) printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",max1,index);    else printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",max2,index1,index2);}int main(){    while(scanf("%d%d", &n, &t)&&(n||t))    {        init();        printf("System #%d\n", c++);        dijkstra(1);    }    return 0;}

0 0
原创粉丝点击