poj 1135 基础最短路

版权声明:随意转(虽然不会有人稀罕…… https://blog.csdn.net/qq_33656136/article/details/52276438

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.

这是一道多米诺骨牌的题目  从第一块骨牌出发   问最后一块倒下的骨牌在哪   若是端点   直接输出时间和端点   否则输出时间和   最后的骨牌在哪两块之间


怎么说呢    直接用spfa  算出  从1节点到其他节点的单源最短路径    找出最大的  

因为作为最短路的最大值    它必定是一条路径    就算有其他的路径到达这个节点   那也是肯定逼这条最短路径要慢的  

所以我们从这个最大节点出发   除了最短路径的其他路径都会是没有走过的路径(也就是还能继续走下去 

详见注释


ac code


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstdlib>

using namespace std;
const int maxn=505;
const int inf=0x3f3f3f3f;
int n,m,idx;
int head[maxn],d[maxn];
bool vis[maxn];
struct edge
{
    int v,val;
    int nxt;
}edges[maxn*maxn];

void init()//初始化
{
    memset(head,-1,sizeof head);
    memset(vis,false,sizeof vis);
    memset(d,0x3f,sizeof d);
    idx=0;
}

void add(int u,int v,int val)//邻接表存储
{
    edges[idx].v=v;
    edges[idx].val=val;
    edges[idx].nxt=head[u];
    head[u]=idx++;

    edges[idx].v=u;
    edges[idx].val=val;
    edges[idx].nxt=head[v];
    head[v]=idx++;
}

void spfa()//spfa算法   都是套路
{
    queue<int> que;
    que.push(1);
    vis[1]=true;
    d[1]=0;
    while(!que.empty())
    {
        int tmp=que.front();
        que.pop();
        vis[tmp]=false;
        for(int u=head[tmp];u!=-1;u=edges[u].nxt)
        {
            int v=edges[u].v;
            if(d[tmp]+edges[u].val<d[v])
            {
                d[v]=d[tmp]+edges[u].val;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
}

int main()
{
    int u,v,cost,test=0;
    while(scanf("%d %d",&n,&m),n||m)
    {
        init();
        while(m--)
        {
            scanf("%d %d %d",&u,&v,&cost);
            add(u,v,cost);
        }
        spfa();
        double tim=0.0;//初始化为0.0  避免考虑只有一个多米诺骨牌的情况
        int domino=1;
        for(int i=2;i<=n;i++)
        {
            if(tim<d[i])//找到最大的最短路径   并记录
            {
                tim=(double)d[i];
                domino=i;
            }
        }
        int le=domino;
        double tt=tim;
        bool flag=true;
        for(u=head[domino];u!=-1;u=edges[u].nxt)//从该节点出发
        {
            if(d[u]+edges[u].val!=d[domino])//如果不是最短路径
            {
                v=edges[u].v;
                double tmp=(double)(edges[u].val-(d[domino]-d[v]))/2.0;//计算这条路径上还能走多久
                if(tt<tmp+tim)
                {
                    tt=tim+tmp;
                    le=v;
                    flag=false;
                }
            }
        }
        if(le>domino) swap(le,domino);
        printf("System #%d\n",++test);
        if(flag) printf("The last domino falls after %.1lf seconds, at key domino %d.\n\n",tim,domino);
        else printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n\n",tt,le,domino);
    }
    return 0;
}

最近在水最短路………………最短路熟悉后再学差分约束

唉   高级搜索还没怎么写   暑假就快结束了

然而集训还有很长时间=_=||












没有更多推荐了,返回首页