POJ 1062 昂贵的聘礼

来源:互联网 发布:ftp服务器默认端口号 编辑:程序博客网 时间:2024/04/30 10:06

POJ 1062 昂贵的聘礼

[★★★☆☆]图论 最短路

  • 题目大意:

    中文题。。不过题目介绍好捉急啊

  • 样例

    输入:
    1 4
    10000 3 2
    2 8000
    3 5000
    1000 2 1
    4 200
    3000 2 1
    4 200
    50 2 0

    输出:
    5250

  • 解题思路:

    难度在如何处理等级的问题上。。题目介绍真的醉人。
    我用的Bellman算法,不过由于没有负圈,用Dijkstra算法时间复杂度低一点。

  • 代码

#include <iostream>#include <algorithm>using namespace std;const int INF = 1e9 + 7;struct edge{    int from, to, cost;};edge E[10007];int cte;int M, N;int d[105];int dc[105];int L[105];void cpd() {    for (int i = 1; i <= N; i++) {        d[i] = dc[i];    }}void add_edge(int f, int t, int c) {    edge et = {f, t, c};    E[cte++] = et;}int main() {    int ans = INF;    cte = 0;    cin >> M >> N;    int p, l, n, f, c;    int now_l = 0;    cin >> p >> l >> n;    dc[1] = p;    L[1] = l;    for (int j = 0; j < n; j++) {        cin >> f >> c;        add_edge(f, 1, c);    }    for (int i = 2; i <= N; i++) {        cin >> p >> l >> n;        L[i] = l;        dc[i] = p;        for (int j = 0; j < n; j++) {            cin >> f >> c;            add_edge(f, i, c);        }    }    for (int i = 1; i <= N; i++) {        if (L[i] >= L[1] && L[i] <= L[1] + M) {            now_l = L[i];        }        else continue;        cpd();        while (1) {            bool flag = 1;            for (int j = 0; j < cte; j++) {                edge e = E[j];                if ((L[e.from] >= now_l - M && L[e.from] <= now_l) && d[e.to] > d[e.from] + e.cost) {                    d[e.to] = d[e.from] + e.cost;                    flag = 0;                }            }            if (flag) break;        }        if (ans > d[1]) ans = d[1];    }    cout << ans << endl;    return 0;}/* 0 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0*/
0 0