Magical Girl Haze (分层图 + 最短路径优先队列优化)

There are NNN cities in the country, and MMM directional roads from uuu to v(1≤u,v≤n)v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance cic_ici​. Haze is a Magical Girl that lives in City 111, she can choose no more than KKK roads and make their distances become 000. Now she wants to go to City NNN, please help her calculate the minimum distance.

Input

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

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

Then the following MMM lines each line has three integers, describe a road, Ui,Vi,CiU_i, V_i, C_iUi​,Vi​,Ci​. There might be multiple edges between uuu and vvv.

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

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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

typedef long long ll;

const int maxn = 500010;

ll n, m, k, top, head[maxn * 11];
ll dist[maxn * 11];
bool vis[maxn * 11];

struct Edge{
  ll v, w;
  ll next;
}edge[maxn * 11];

struct node{
  ll id;
  ll dis;
   bool operator<(const node &a)const{
        return dis > a.dis;
    }
};

 void add(ll u, ll v, ll w)
{
   edge[top].v = v;
   edge[top].w = w;
   edge[top].next = head[u];
   head[u] = top++;
}


 void Dijkstra()
{
   node s;
   priority_queue < node > q;
   memset(vis, false, sizeof(vis));
   memset(dist, 125, sizeof(dist));
   dist[1] = 0;
   s.id = 1;
   s.dis = 0;
   q.push(s);
   while(!q.empty())
   {
       ll index = q.top().id;
       q.pop();
       vis[index] = true;
       for(ll i = head[index]; i != -1; i = edge[i].next)
       {
           	ll v = edge[i].v;
	   		ll w = edge[i].w;
	   		if(vis[v])
	     		continue;
           if(dist[v] > dist[index] + w)
	       {
              dist[v] = dist[index] + w;
              s.id = v;
              s.dis = dist[v];
              q.push(s);
	   	   }
       }
   }
}



int main()
{
   int t;
   scanf("%d", &t);
   while(t--)
   {
   	scanf("%lld%lld%lld", &n, &m, &k);
	top = 0;
	memset(head, -1, sizeof(head));
	ll u, v, w;
	for(ll i = 1; i <= m; ++ i)
	{
	      scanf("%lld%lld%lld", &u, &v, &w);
	      for(ll j = 0; j <= k; ++ j)
	      {
	           add(u + n * j, v + n * j, w);
		       if(j != k)
		       {
		   	     add(u + n * j, v + n * (j + 1), 0);
		       }
	      }
	}
	Dijkstra();
	printf("%lld\n", dist[n * (k + 1)]);
   }
   return 0;

}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/82355073
今日推荐