1. 程式人生 > >2018 icpc區域賽南京網路賽 L題(分層最短路)

2018 icpc區域賽南京網路賽 L題(分層最短路)

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

Input

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

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

Then the following MM lines each line has three integers, describe a road, U_i, V_i, C_iUi​,Vi​,Ci​. There might be multiple edges between uu and vv.

It is guaranteed that N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0 \le C_i \le 1e90≤Ci​≤1e9. There is at least one path between City 11 and City NN.

Output

For each test case, print the minimum distance.

樣例輸入複製

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

樣例輸出複製

3

最開始真的是膨脹了,覺得最短路的題都刷過了,真的是被捶的頭皮發麻,原來最短路的變化這麼的多。

這個題就是可以使kt條邊的權值為0,求出1~n的最短路。

附上程式碼:

#include<stdio.h>
#include<bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
const int maxn = 2e5 + 7;
const long long  inf = 1e18 + 7;
struct node
{
	int v;
	int w;
	int next;
}edge[2 * maxn];
struct qnode
{
	int u;
	int k;    //次數
	long long dis;
	bool operator < (const qnode &p)const {
		return dis > p.dis;
	}
	qnode(int _u, int _k, long long _dis)
	{
		u = _u;
		k = _k;
		dis = _dis;
	}
};
int head[maxn];
int tot = 0;
long long  dis[maxn][20];
int t, n, m;
int k;
void add(int u, int v, int w)
{
	edge[tot].v = v;
	edge[tot].w = w;
	edge[tot].next = head[u];
	head[u] = tot++;
	return;
}
void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
	return;
}
long long dij()
{
	priority_queue<qnode>q;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= k; j++)
		{
			dis[i][j] = inf;
		}
	}
	dis[1][0] = 0;
	q.push(qnode(1, 0, 0));
	while (!q.empty())
	{
		int u = q.top().u;
		int tmp_k = q.top().k;
		if (u == n)
		{
			return dis[u][tmp_k];
		}
		q.pop();
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].v;
			int w = edge[i].w;
			if (dis[u][tmp_k] + w < dis[v][tmp_k])
			{
				dis[v][tmp_k] = dis[u][tmp_k] + w;
				q.push(qnode(v, tmp_k, dis[v][tmp_k]));
			}
			if (tmp_k != k)
			{
				if (dis[v][tmp_k + 1] > dis[u][tmp_k])
				{
					dis[v][tmp_k+1] = dis[u][tmp_k];
					q.push(qnode(v, tmp_k + 1, dis[v][tmp_k + 1]));
				}
			}
		}
	}
	return -1;
}
int main()
{
	//freopen("C://input.txt", "r", stdin);
	scanf("%d", &t);
	while (t--)
	{
		init();
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 0; i < m; ++i)
		{
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			add(u, v, w);
		}
		printf("%d\n", dij());
	}
	return 0;
}


dij加dp思想

相關推薦

2018 icpc區域南京網路 L分層短路

There are NN cities in the country, and MMdirectional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici​. Haz

2018-南京網絡icpc-L分層短路

cpp ans main -- pri long long cst pre return 題意:給你n個點,m條邊的有向帶權圖,然後你每次可以選<=k條邊的邊權變成0,問你1到n的最短路; 解題思路:這道題基本上就是原題了呀,bzoj2763(無向圖),解法就是拆點跑

2018 ACM-ICPC南京網路 Magical Girl Haze分層短路

There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici​. Haz

ACM-ICPC 2018 南京賽區網路預賽 L. Magical Girl Hazedijkstra+分層短路

思路來源 https://blog.csdn.net/zhangche0526/article/details/62881066 題意 給你n個城市,m條邊, 共有k次免費機會,可以將其中k條邊的權值變為0, 求點1到點n的最短路。 題解 (百度 分層圖最短路

2018 ICPC 沈陽網絡預賽 Fantastic Graph 優先隊列

max print 找到 需要 ack 子圖 我們 span ret 【傳送門】https://nanti.jisuanke.com/t/31447 【題目大意】:有一個二分圖,問能不能找到它的一個子圖,使得這個子圖中所有點的度數在區間【L,R】之內。 【題解】首先我

2015年ACM/ICPC上海賽區 L數論 LCM推理

題意: 開始時有一個格子(x,y),每次移動會移動到(x,y+lcm(x,y))或(x+lcm(x,y),y),現在給你最終的點,讓你求可能的起點個數。 x,y<=1e9 比賽的時候石樂志。。。lcm推理一下。 假設前一個格子是(x,y) ,令 x=a*

2018南京網路 L Magical Girl Haze短路、堆、狀態轉移

我們設定dis[i][k]表示走到第i號點,免費經過了k條邊的最短路。  對於我們當前找到的終點,嘗試起點的狀態去更新,不選擇此條邊免費的狀態和選擇此條邊免費的狀態,再將這兩個狀態壓入佇列去更新可以到達的其他狀態。 #include<cstdio> #include&l

2018 icpc-南京網路

L . Magical Girl Haze  題目連結:  https://nanti.jisuanke.com/t/31001 題解:分層圖-最短路(拆點建圖),這篇部落格寫的很詳細,包括整個思考的過程----https://www.cnblogs.co

2018 icpc 南京網路

題目:連結 A. An Olympian Math Problem 輸出n-1即可(女朋友猜的)。 #include<bits/stdc++.h> using namespace std; #define ll long long ll fac[103]; int mai

分層短路】dijkstra講解+複習 /2018 ACM-ICPC南京網路 Magical Girl Haze

首先,HDU-2544 來回顧一下dij這個東西(連結: 這裡) 講解: (來源:坐在馬桶上學演算法) 不斷對邊進行鬆弛 (以下為copy) 演算法的基本思想是:每次找到離源點(上面例子的源點就是 1 號頂點)最近的一個頂點,然後以該頂點為中心進行擴充套件,最終得到

2018 icpc南京網路G(線段樹)

思路:因為只有1e5的資料。我們可以模擬一下所有情況全算出來,把答案存下來即可。但會一遍一遍的遍歷超時,我們可以優化遍歷的方式,因為只有1e5的房間,我們每次找的房間都是小於當前新燈泡數且最前面的房間。這個查詢用一顆線段樹既可以解決。 我們每次找到一個滿足的房間就讓當前燈泡

分層圖/ 變形短路2018 ACM-ICPC 南京網路

題意: 給出一張有向圖,能夠把最多k條邊的權值變為零,問從點1到點n的最短距離是多少。 題解一: 分層圖。 建立k張一模一樣的有向圖,層與層之間用權值為零的邊相連。 層與層之間的跳躍就是代表選擇了一條邊權值變為零。 跑一遍迪傑斯特拉,輸出最後一層的點n距離就是

2018南京網路L Magical Girl Haze 分層短路

題目大意: 給你n個點,m條邊,讓你最多可以使k條邊的權值為0,問你1到n的最短路。 分析: dijsktra的距離陣列可以多開一維,記錄已經讓幾條邊的權值為0,即分層圖最短路 #include <iostream> #include <alg

2018南京網路 j sum篩法、非平方數相乘

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #incl

ACM/ICPC 2018亞洲區預選賽北京賽站網路 C - Cheat

棋牌室成員+1 #include<bits/stdc++.h> using namespace std; #define For(i,a,b) for (int i=a;i<=b;i++) map<string,int> own[4]; map<

ACM/ICPC 2018亞洲區預選賽北京賽站網路 D - 80 Days

要求的點滿足特定性質 對a[i]-b[i]做字首和s[i] 滿足從該點開始往後n個點的最小s不會比起點的s小超過c 只需滿足這個條件就付的起錢 #include<bits/stdc++.h> using namespace std; #define For(i,a,b)

ACM/ICPC 2018亞洲區預選賽北京賽站網路 Tomb Raider

時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond

ACM/ICPC 2018亞洲區預選賽北京賽站網路 K-Dimensional Foil II(思維)

時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB   描述 "K-Dimensional Foil" is a dimensional weapon. Its function is quite easy: It can ascend a re

2018年 ACM/ICPC亞洲區域 青島賽區現場 FZOJ 4063 思維

DreamGrid, the king of Gridland, is making a knight tournament. There are  knights, numbered from 1 to , participating in the tournamen

計蒜客 2018南京網路 I Skr迴文樹

題目:給一串由0..9組成的數字字串,求所有不同迴文串的權值和。比如說“1121”這個串中有“1”,“2”,“11”,“121”三種迴文串,他們的權值分別是1,2,11,121。最終輸出ans=135。 思路:之前寫了馬拉車的演算法,網上看到的這個題是迴文樹的模板題。。。