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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值