ACM-ICPC 2018 南京赛区网络预赛 L-Magical Girl Haze

There are N cities in the country, and MMdirectional roads from uu to v(1 ≤ u, v ≤ n). Every road has a distance ci​. Haze is a Magical Girl that lives in City 1, she can choose no more than KK roads and make their distances become 0. Now she wants to go to City N, please help her calculate the minimum distance.

Input

The first line has one integer T(1 ≤ T ≤ 5), then following T cases.

For each test case, the first line has three integers N,M and K.

Then the following MM lines each line has three integers, describe a road Ui​, Vi​, Ci​. There might be multiple edges between u and v.

It is guaranteed that N ≤ 100000, M ≤ 200000, K ≤ 10, 0 ≤ Ci ​≤ 1e9.

There is at least one path between City 1 and City N.

Output

For each test case, print the minimum distance.

样例输入

1
5 6 1
1 2 2
1 3 4
2 4 3

Hint

分层图,dij不用特判重边,建的是无向图!!!!

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <set>
#include <cmath>
#include <sstream>
#include <stack>
#include <fstream>
#include <ctime>
#pragma warning(disable:4996);
#define mem(sx,sy) memset(sx,sy,sizeof(sx))
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const double PI = acos(-1.0);
const ll llINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
using namespace std;
#define pa pair<int, int>
#define ppa pair<ll, pa>
//const int mod = 1e9 + 7;
const int maxn = 100005;
const int maxm = 200005;
struct edge {
	int w;
	int to, next;
}edges[maxm << 1];
int n, m, k;
int head[maxn], cnt;
ll dist[maxn][25];
bool vis[maxn][25];

void addedge(int u, int v, int w) {
	edges[++cnt].next = head[u];
	edges[cnt].w = w;
	edges[cnt].to = v;
	head[u] = cnt;
}

void init() {
	cnt = 0;
	mem(head, -1);
	mem(vis, 0);
	mem(dist, llINF);
}

void dij() {
	priority_queue<ppa, vector<ppa>, greater<ppa> > Q;
	dist[1][0] = 0;
	Q.push(ppa(0, pa(1, 0)));
	while (!Q.empty()) {
		ppa tmp = Q.top(); Q.pop();
		int u = tmp.second.first;
		int d = tmp.second.second;
		vis[u][d] = 1;
		for (int i = head[u]; ~i; i = edges[i].next) {
			int v = edges[i].to;
			ll w = edges[i].w;
			if (dist[v][d] > dist[u][d] + w) {
				dist[v][d] = dist[u][d] + w;
				if (!vis[v][d]) {
					Q.push(ppa(dist[v][d], pa(v, d)));
				}
			}
			if (dist[v][d + 1] > dist[u][d] && d + 1 <= k) {
				dist[v][d + 1] = dist[u][d];
				if (!vis[v][d + 1]) {
					Q.push(ppa(dist[v][d + 1], pa(v, d + 1)));
				}
			}
		}
	}
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		init();
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 0, u, v, w; i < m; i++) {
			scanf("%d%d%d", &u, &v, &w);
			addedge(u, v, w);
		}
		dij();
		ll ans = llINF;
		for (int i = 0; i <= k; i++) {
			ans = min(dist[n][i], ans);
		}
		printf("%lld\n", ans);
	}
}
3 4 1
3 5 6
4 5 2

样例输出

3

猜你喜欢

转载自blog.csdn.net/qq_23502651/article/details/82314395
今日推荐