poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135

建模分两部分:1、如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路

2、如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置

到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种

上代码

/**********************************    poj 1135 by pilgrim
    无向图边没来回弄
    少个空行PE....
    2014.6.23
\**********************************/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
#define ll long long

const int SIZE = 500+10;
const int INF = 2000000000;
double e[SIZE][SIZE], d[SIZE];
bool s[SIZE];

int n,m;

void Init()
{
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
            e[i][j]=INF,d[i]=INF;
}

void AddEdge()
{
    int u,v;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);/*最小的下标为0*/
        scanf("%lf",&e[u-1][v-1]);
        e[v-1][u-1]=e[u-1][v-1];
    }
}

void dij(int cnt)
{
    for(int i=0;i<n;i++)
    {
        d[i]=e[0][i];
        s[i]=0;
    }
    d[0]=0,s[0]=1;
    for(int i=0;i<n-1;i++)
    {
        int mmin = INF,u=0;
        for(int j=0;j<n;j++)
            if(!s[j] && d[j] <mmin)
            {
                u=j;mmin=d[j];
            }
        s[u]=1;
        for(int k=0;k<n;k++)
        {
            if(!s[k] && e[u][k] < INF && d[u]+e[u][k]<d[k])
                d[k]=d[u]+e[u][k];
        }
    }
    double maxt1=-INF,maxt2=-INF,tmp;
    int pos,poss,pose;
    for(int i=0;i<n;i++)
        if(maxt1<d[i])
        {
            maxt1=d[i];
            pos=i;
        }
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            if(d[i] <INF && d[j] < INF && e[i][j] < INF)
            {
                tmp=(d[i]+d[j]+e[i][j])/2.0;//
                if(tmp>maxt2)
                {
                    maxt2=tmp;
                    poss=i,pose=j;
                }
            }
        }
    printf("System #%d\n",cnt);
    if(maxt2>maxt1)printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",maxt2,poss+1,pose+1);
    else printf("The last domino falls after %.1lf seconds, at key domino %d.\n",maxt1,pos+1);
    putchar('\n');
}
int main()
{
    int ncase=1;
    while(~scanf("%d%d",&n,&m), n+m)
    {
        Init();
        AddEdge();
        dij(ncase++);
    }
    return 0;
}

poj 1135 最短路 dijkstra

时间: 2024-10-15 14:31:52

poj 1135 最短路 dijkstra的相关文章

POJ 1135 Domino Effect (Dijkstra 最短路)

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 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 t

MPI Maelstrom POJ - 1502 最短路Dijkstra

BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swigert, has asked her to benchmark the

POJ 2387 最短路Dijkstra算法

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33607   Accepted: 11383 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n,树根为编号1,选择一些边,使得所有节点构成一棵树,选择边的代价是(子孙的点的重量)×(这条边的价值).求代价最小多少. 分析: 单看每个点被计算过的代价,很明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 不过坑点还是很多的. 点的数量高达5w个,用矩阵存不行,只能用边存. 还有路径和结

POJ 2449 Remmarguts&#39; Date ( Dijkstra + A* 求解第K短路 )

#include <iostream> #include <cstring> #include <queue> #include <fstream> using namespace std; #define E 100005 #define V 1005 #define INF 1 << 30 int heads[V], r_heads[V]; int dists[V]; bool visits[V]; int nEdgeNum, nNodeNu

POJ 2394 Checking an Alibi (最短路+Dijkstra)

Checking an Alibi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6217   Accepted: 2257 Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's cows. FJ is trying to determine which of his C (1

POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30007   Accepted: 10092 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

poj 2253 Frogger (dijkstra最短路)

题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25773   Accepted: 8374 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on an