Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

特别要注意n=1,应该输出0.0 1

Posted by 13408100238 at 2015-08-20 11:38:59 on Problem 1135
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const LL INF = 0x5fffffff;
const double EXP = 1E-9;
const LL MOD = (LL)1E9+7;
const int MS = 505;

// poj 1135

int dis[MS];
int edges[MS][MS];
int flag[MS];
int n, m, kase = 1;

void SP(int s) {
    for (int i = 1; i <= n; i++) {
        dis[i] = edges[s][i];
        flag[i] = 0;
    }
    flag[s] = 1;
    dis[s] = 0;
    for (int i = 1; i < n; i++) {
        int minv = INF;
        int u = -1;
        for (int v = 1; v <= n; v++) {
            if (flag[v] == 0 && minv > dis[v]) {
                minv = dis[v];
                u = v;
            }
        }
        flag[u] = 1;
        for (int v  = 1; v <= n; v++) {
            if (flag[v] == 0 && edges[u][v] < INF && dis[v] > dis[u] + edges[u][v]) {
                dis[v] = dis[u] + edges[u][v];
            }
        }
    }
    int maxv1 = 0;
    int pos = 1;
    for (int i = 1; i <= n; i++) {
        if (dis[i] > maxv1) {
            maxv1 = dis[i];
            pos = i;
        }
    }
    double maxv2 = 0;
    int pos1 = 1, pos2 = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            double t = (dis[i] + dis[j] + edges[i][j]) / 2.0;
            if (edges[i][j] < INF && maxv2 < t) {
                maxv2 = t;
                pos1 = i;
                pos2 = j;
            }
        }
    }
    printf("System #%d\n", kase++);
    if ((maxv1 + EXP ) > maxv2)
        printf("The last domino falls after %.1lf seconds, at key domino %d.\n", (double)maxv1, pos);
    else
        printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n",
        maxv2, pos1, pos2);
    printf("\n");
}


int main() {
    while (scanf("%d%d", &n, &m) == 2 && (n + m)) {
        int u, v, w;
        for (int i = 0; i <= n; i++)
            fill(edges[i], edges[i] + n + 1, INF);
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &u, &v, &w);
            edges[u][v] = edges[v][u] = w;
        }
        SP(1);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator