Lightoj 1281 (二维最短路)

题解

给你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

你可能感兴趣的:(Lightoj 1281 (二维最短路))