1. 程式人生 > >2018 ACM-ICPC南京網路賽 Magical Girl Haze(分層最短路)

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​. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances become 00. 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

邊去重+分層最短路

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005;
const int MAXM = 200005;
int k;
struct node1
{
    int u,v;
    ll w;
}e[MAXM];
bool cmp(struct node1 a,struct node1 b)
{
    if(a.u == b.u && a.v == b.v) return a.w < b.w;
    else if(a.u == b.u) return a.v < b.v;
    return a.u < b.u;
}
struct node2
{
    int to,Next;
    ll w;
}edge[MAXM];
int tot,head[MAXN];
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,ll w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].Next = head[u];
    head[u] = tot++;
}
struct node3
{
    int x,y;
    ll dis;
    node3(){}
    node3(int _x,int _y,ll _dis)
    {
        x = _x;
        y = _y;
        dis = _dis;
    }
    friend bool operator < (const node3 a,const node3 b)
    {
        return a.dis > b.dis;
    }
};
ll dis[MAXN][15];
bool vis[MAXN][15];
void dijkstra()
{
    memset(vis,false,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    dis[1][0] = 0;
    priority_queue<node3> pq;
    pq.push(node3(1,0,0));
    while(!pq.empty()) {
        struct node3 temp = pq.top();
        pq.pop();
        int x = temp.x;
        int y = temp.y;
        if(vis[x][y]) continue;
        vis[x][y] = true;
        for(int i = head[x]; i != -1; i = edge[i].Next) {
            int to = edge[i].to;
            if(dis[x][y] + edge[i].w < dis[to][y]) {
                dis[to][y] = dis[x][y] + edge[i].w;
                pq.push(node3(to,y,dis[to][y]));
            }
            if(y + 1 <= k && dis[x][y] < dis[to][y + 1]) {
                dis[to][y + 1] = dis[x][y];
                pq.push(node3(to,y + 1,dis[to][y + 1]));
            }
        }
    }
}
int main(void)
{
    int T,n,m;
    ll ans;
    scanf("%d",&T);
    while(T--) {
        init();
        scanf("%d %d %d",&n,&m,&k);
        for(int i = 1; i <= m; i++) {
            scanf("%d %d %lld",&e[i].u,&e[i].v,&e[i].w);
        }
        sort(e + 1,e + 1 + m,cmp);
        int preu = -1,prev = -1;
        for(int i = 1; i <= m; i++) {
            if(!(e[i].u == preu && e[i].v == prev)) {
                addedge(e[i].u,e[i].v,e[i].w);
                preu = e[i].u;
                prev = e[i].v;
            }
        }
        dijkstra();
        ans = 1e18;
        for(int i = 0; i <= k; i++) ans = min(ans,dis[n][i]);
        printf("%lld\n",ans);
    }
    return 0;
}

相關推薦

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

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

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

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

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

計蒜客 Transport Ship 2018-ACM-ICPC-焦作-網路DP

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of

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 ACM-ICPC北京網路 A.Saving Tang Monk II(bfs)

好久沒bfs了,沒想到用三維陣列去標記狀態來進行bfs。。。但仔細一想想,這麼解很有道理。。。 因為這個圖每個格子可以走多遍,我們考慮,對於一個格子,如果帶著相同的氧氣瓶走兩次,那結果是相同的。所以我們從這個約束進行搜尋,開一個三位數字,vis[i][j][k]代表帶著k個

2018 ACM-ICPC 瀋陽網路C Convex hull

考慮杜教篩就跑偏了… 定義gay(i)=i2∗μ2(i)gay(i)=i2∗μ2(i) ∑ni=1∑ij=1gay(i)=∑ni=1(n−i+1)gay(i)=∑ni=1(n−i+1)i2μ2(i)∑i=1n∑j=1igay(i)=∑i=1n(n−i+1

The 2018 ACM-ICPC上海大都會 J Beautiful Numbers 數位DP

mes div spa ems urn 余數 limit style 狀態 題意:求小於等於N且能被自己所有位上數之和整除的數的個數。 分析:裸的數位dp。用一個三位數組dp[i][j][k]記錄:第i位,之前數位之和為j,對某個mod余數為k的狀態下滿足條件的個數。這裏m

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

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

2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)蒟蒻解題全部都是水題和思維題

2018.5.14:還有三個沒有補,一個然顏色種類那個,一個求fn的,一個尺取能寫的題(估計是個簡單dp) http://codeforces.com/gym/101652 A problemN 給定一個字串,問你判斷該字串的所有迴文字串(注意單詞cons

L. Magical Girl Haze 分層短路

分層圖最短路的裸題。 #include<iostream> #include<stdio.h> #include<queue> #define LL

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

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

ACM-ICPC 2018 南京賽區網路預賽】Magical Girl Haze分層圖】

Description:    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 dis

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

2018ACM-ICPC 南京現場 A.Adrien and Austin

題意 有一堆數量為N的石子,石子編號從1⋯N1\cdots N1⋯N排成一列,兩個人玩遊戲,每次可以取1⋯K1\cdots K1⋯K個連續編號的石子,Adrien先手,如果有誰不能取了則他輸,兩個人為A

南京網路 L Magical Girl Haze優先佇列+短路

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

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

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

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

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