Lightoj 1281 (二维最短路)

本文介绍了一种在有限新增道路条件下寻找两城市间最短路径的方法。通过使用优先队列和多维度距离矩阵,能够在预算限制下高效找到最优路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题解

给你N个点,M条已有的边,K条可以添加的边,K条中最多只能选择D条,问0到N-1的最短路,多开一维记录走到该点选择了多少条路即可

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 10010;
const int M = 30010;
const int INF = 0x3f3f3f3f;

struct Edge{
    int u, v, mark, dis, next;
    Edge() {}
    Edge(int u, int v, int mark, int dis, int next): u(u), v(v), mark(mark), dis(dis), next(next){}
}E[M];

struct Node {
    int u, dis, times;
    Node() {}
    Node(int u, int dis, int times): u(u), dis(dis), times(times) {}
    bool operator < (const Node &a) const {
        return dis > a.dis;
    }
};

int head[N], d[N][12];
int n, m, k, t, cas = 1;
bool vis[N][12];

void init() {
    memset(head, -1, sizeof(head));
    scanf("%d%d%d%d", &n, &m, &k, &t);
    int u, v, dis;
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &dis);
        E[i] = Edge(u, v, 0, dis, head[u]);
        head[u] = i;
    }

    for (int i = 0; i < k; i++) {
        scanf("%d%d%d", &u, &v, &dis);
        E[i + m] =  Edge(u, v, 1, dis, head[u]);
        head[u] = i + m;
    }
}

void solve() {
    priority_queue<Node> Q;
    memset(d, 0x3f, sizeof(d));
    memset(vis, 0 ,sizeof(vis));
    d[0][0] = 0;
    Q.push(Node(0, 0, 0));
    while (!Q.empty()) {
        int u = Q.top().u;
        int dis = Q.top().dis;
        int times = Q.top().times;
        Q.pop();
        if (vis[u][times]) continue;
        vis[u][times] = true;

        for (int i = head[u]; ~i; i = E[i].next) {
            int v = E[i].v;
            if (times + E[i].mark > t) continue;
            if (d[v][times + E[i].mark] > d[u][times] + E[i].dis) {
                d[v][times + E[i].mark] = d[u][times] + E[i].dis;
                Q.push(Node(v, d[v][times + E[i].mark], times + E[i].mark));
            }
        }
    }
    int ans = INF;
    for (int i = 0; i <= t; i++)
        ans = min(d[n - 1][i], ans);
    if (ans == INF) printf("Case %d: Impossible\n", cas++);
    else printf("Case %d: %d\n", cas++, ans);
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        init();
        solve();
    }
    return 0;
}

题目

The country - Ajobdesh has a lot of problems in traffic system. As the Govt. is very clever (!), they made a plan to use only one way roads. Two cities s and t are the two most important cities in the country and mostly people travel from s to t. That’s why the Govt. made a new plan to introduce some new one way roads in the traffic system such that the time to travel from s to t is reduced.

But since their budget is short, they can’t construct more than d roads. So, they want to construct at most d new roads such that it becomes possible to reach t from s in shorter time. Unluckily you are one living in the country and you are assigned this task. That means you will be given the existing roads and the proposed new roads, you have to find the best path from s to t, which may allow at most d newly proposed roads.

Input
Input starts with an integer T (≤ 30), denoting the number of test cases.

Each case starts with a line containing four integers n (2 ≤ n ≤ 10000), m (0 ≤ m ≤ 20000), k (0 ≤ k ≤ 10000), d (0 ≤ d ≤ 10) where n denotes the number of cities, m denotes the number of existing roads and k denotes the number of proposed new roads. The cities are numbered from 0 to n-1 and city 0 is denoted as s and city (n-1) is denoted as t.

Each of the next m lines contains a description of a road, which contains three integers ui vi wi (0 ≤ ui, vi < n, ui ≠ vi, 1 ≤ wi ≤ 1000) meaning that there is a road from ui to vi and it takes wi minutes to travel in the road. There is at most one road from one city to another city.

Each of the next k lines contains a proposed new road with three integers ui vi wi (0 ≤ ui, vi < n, ui ≠ vi 1 ≤ wi ≤ 1000) meaning that the road will be from ui to vi and it will take wi minutes to travel in the road. There can be at most one proposed road from one city to another city.

Output
For each case, print the case number and the shortest path cost from s to t or “Impossible” if there is no path from s to t.

Sample Input
2
4 2 2 2
0 1 10
1 3 20
0 2 5
2 3 14
2 0 1 0
0 1 100
Sample Output
Case 1: 19
Case 2: Impossible

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值