【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 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 <= 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 <= 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
3 4 1
3 5 6
4 5 2

样例输出

3

题意:

      给一张有向图,给一个k,可以将图中k条边的边权变为0,求从1->n的最短路径。

解法:

      由于 k<=10,由此考虑分层图做法。

扫描二维码关注公众号,回复: 3011185 查看本文章

      给每个点建10个分身,dis[i][j] 表示从 城市1 到达城市 i,一共改了 j 条边的最短路径。

      其余操作和正常最短路求法一致,更新的时候有两种更新方法。假设队列弹出的节点是 dis[i][j],y为i可达到的点。

    则用 dis[i][j] 去更新 dis[y][j] 和 dis[y][j+1],相当于扩点操作,其余操作和dij模板一致。

      如此操作即可求出答案。

总结:

      在图论的问题中,分层图问题十分常见,不单可以出现在最短路中,还可以出现在网络流、Tarjan等各类问题之中。

      重点在于题中修改 点权 或 边权 的次数有限,因此重点在于观察题中的数据范围,能够及时地想到分层图知识。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define rep(i,a,b) for(int i = a;i <= b;i++)
using namespace std;
const int N = 1e5+100;
const int M = 5*1e5+10;
typedef long long ll;
const ll inf = 1e15;

struct Edge{
	int to,next,w;
}e[M];
int n,m,k,head[N],tot;
ll dis[N][15];
int vis[N][15];

void init()
{
	tot = 1;
	rep(i,0,n) head[i] = 0;
}

void add(int x,int y,int z)
{
	e[++tot].to = y, e[tot].next = head[x], head[x] = tot, e[tot].w = z;
}

struct Point{
	ll ans;
	int u,cnt;
}thp;

bool operator < (Point a,Point b)
{
	return a.ans > b.ans;
}

priority_queue<Point> q;

void dijstra(int s)
{
	while(q.size()) q.pop();
	thp.ans = 0, thp.u = s, thp.cnt = 0;
	q.push(thp);
	rep(i,1,n)
		rep(j,0,k) vis[i][j] = 0;
	rep(i,1,n) 
		rep(j,0,k) dis[i][j] = inf;
	dis[s][0] = 0;
	while(q.size())
	{
		thp = q.top();
		q.pop();
		int u = thp.u;
		int x = thp.cnt;
		if(vis[u][x]) continue;
		vis[u][x] = 1;
		for(int i = head[u]; i ; i = e[i].next)
		{
			int y = e[i].to;
			if(!vis[y][x]){
				if(dis[y][x] > dis[u][x]+e[i].w){
					dis[y][x] = dis[u][x] + e[i].w;
					thp.u = y, thp.ans = dis[y][x], thp.cnt = x;
					q.push(thp);
				}
			}
			if(!vis[y][x+1] && (x < k)){
				if(dis[y][x+1] > dis[u][x]){
					dis[y][x+1] = dis[u][x];
					thp.u = y, thp.ans = dis[y][x+1], thp.cnt = x+1;
					q.push(thp);
				}
			}
		} 	
	}
}

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%d%d%d",&n,&m,&k);
		rep(i,1,m)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
		}
		dijstra(1);
		printf("%lld\n",dis[n][k]);
	}	
	return 0;
}

/*
	题意:
给一张有向图,给一个k,可以将图中k条边的边权变为0,求从1->n的最短路径
	解法:
由于 k<=10,由此考虑分层图做法
给每个点建10个分身,dis[i][j]表示从城市1到达城市i,一共改了j条边的最短路径
其余操作和正常最短路求法一致,更新的时候有两种更新方法。假设队列弹出的节点是dis[i][j],y为i可达到的点
则用dis[i][j]去更新dis[y][j]和dis[y][j+1],相当于扩点操作,其余操作和dij模板一致
如此操作即可求出答案
*/

猜你喜欢

转载自blog.csdn.net/qq_41552508/article/details/82290193