Poj 1135 domino effect

Source: Internet
Author: User

The most short-circuit problem, I use spfa.

Obtain the shortest time of each vertex.

Then the length of each edge is (d [u] + d [v] + Len)/2 Len.

Find the longest time.


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>using namespace std;int n,m;struct lx{    int v;    double t;};vector<lx> g[501];double d[501];bool vis[501];void SPFA(){    for(int i=0;i<=n;i++)        d[i]=100000000.0,vis[i]=0;    queue<int>q;    d[1]=0,vis[1]=1,q.push(1);    while(!q.empty())    {        int u=q.front();q.pop();        vis[u]=0;        for(int j=0;j<g[u].size();j++)        {            int v=g[u][j].v;            double t=g[u][j].t;            if(d[v]>d[u]+t)            {                d[v]=d[u]+t;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    int p,p1,p2;    double t, t1=-100001.0,t2=-100001.0;    for(int i=1;i<=n;i++)    {        for(int j=0;j<g[i].size();j++)        {            t=(d[i]+d[g[i][j].v]+g[i][j].t)/2;            if(t>t2)                p1=i,p2=g[i][j].v,t2=t;        }    }    for(int i=1;i<=n;i++)        if(d[i]>t1)t1=d[i],p=i;    if(t1>=t2)    {        printf("The last domino falls after %.1f seconds, at key domino %d.\n",t1,p);    }    else    {         printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",t2,p1,p2);    }}int main(){    int cot=1;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==0&&m==0)return 0;        for(int i=0;i<=n;i++)            g[i].clear();        for(int i=0;i<m;i++)        {            int u,v;            lx now;            scanf("%d%d%lf",&u,&v,&now.t);            now.v=v;            g[u].push_back(now);            now.v=u;            g[v].push_back(now);        }        printf("System #%d\n",cot++);        SPFA();        printf("\n");    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.