hdu 1690最短路

就是建图,把题目描述的东东转换成一张图求最短路就行了,因为是随机询问,所以用floyd比较方便。不过打完之后一直WA,百思不得其解的情况下看了一下discuss,有人说得用long long,于是我马上改了交,果然过了……唉,以后看题还是得细心……

/*
 * hdu1690/win.cpp
 * Created on: 2012-10-27
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
const int MAXN = 105;
const LL INF  = 0x7fffffffffffffffLL;
LL graph[MAXN][MAXN];
int N;
void Floyd() {
    int i, j, k;
    for (k = 0; k < N; k++) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                if(graph[i][k] < graph[i][j] - graph[k][j]) {
                    graph[i][j] = graph[i][k] + graph[k][j];
                }
            }
        }
    }
}
void buildgraph(int &M) {
    fill_n((LL *)graph, MAXN * MAXN, INF);
    int L[4], C[4], pos[MAXN];
    for(int i = 0; i < 4; i++) {
        scanf("%d", &L[i]);
    }
    for(int i = 0; i < 4; i++) {
        scanf("%d", &C[i]);
    }
    scanf("%d%d", &N, &M);
    for(int i = 0; i < N; i++) {
        scanf("%d", &pos[i]);
    }
    for(int i = 0; i < N; i++) {
        graph[i][i] = 0;
        for(int j = i + 1; j < N; j++) {
            int t = abs(pos[i] - pos[j]);
            for(int k = 0; k < 4; k++) {
                if(t <= L[k]) {
                    int p = C[k];
                    graph[i][j] = graph[j][i] = p;
                    break;
                }
            }
        }
    }
}


int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int T, M, a, b;
    scanf("%d", &T);
    for(int t = 1; t <= T; t++) {
        printf("Case %d:\n", t);
        buildgraph(M);
        Floyd();
        for(int i = 0; i < M; i++) {
            scanf("%d%d", &a, &b);
            if(graph[a - 1][b - 1] == INF) {
                printf("Station %d and station %d are not attainable.\n", a, b);
            }else {
                printf("The minimum cost between station %d and station %d is %I64d.\n", a, b, graph[a - 1][b - 1]);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/moonbay/p/2742808.html