洛谷-P2939-[USACO09FEB]改造路Revamping Trails(分层最短路)

题目链接:https://www.luogu.org/problem/show?pid=P2939

题意翻译

约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体.

通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路.在高速公路上的通行几乎是瞬间完成的,所以高速公路的通行时间为0.

请帮助约翰决定对哪些小径进行升级,使他每天从1号牧场到第N号牧场所花的时间最短

题目描述

Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= 50,000) trails conveniently numbered 1..M from pasture 1 all the way out to pasture N (a journey which is always possible for trail maps given in the test data). The N (1 <= N <= 10,000) pastures conveniently numbered 1..N on Farmer John's farm are currently connected by bidirectional dirt trails. Each trail i connects pastures P1_i and P2_i (1 <= P1_i <= N; 1 <= P2_i <= N) and requires T_i (1 <= T_i <= 1,000,000) units of time to traverse.

He wants to revamp some of the trails on his farm to save time on his long journey. Specifically, he will choose K (1 <= K <= 20) trails to turn into highways, which will effectively reduce the trail's traversal time to 0. Help FJ decide which trails to revamp to minimize the resulting time of getting from pasture 1 to N.

TIME LIMIT: 2 seconds

输入输出格式

输入格式:

* Line 1: Three space-separated integers: N, M, and K

* Lines 2..M+1: Line i+1 describes trail i with three space-separated integers: P1_i, P2_i, and T_i

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

输出格式:

* Line 1: The length of the shortest path after revamping no more than K edges

输入输出样例

输入样例#1: 复制

4 4 1 
1 2 10 
2 4 10 
1 3 1 
3 4 100 

输出样例#1: 复制

1 

说明

K is 1; revamp trail 3->4 to take time 0 instead of 100. The new shortest path is 1->3->4, total traversal time now 1.

分层最短路,网上有很多大佬的科普了,在这里,这道题已经能够作为一道模板题来运用了;

于是我就学习一波,然后直接写注释吧;

直接一个板子。还是比较简单的。

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);
const int MAXN=1e6+5;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;

struct node{
	int v,w,cost,nxt;
	node(int _v=0,int _w=0,int _nxt=0):
    v(_v),w(_w),nxt(_nxt){}
}edge[MAXN<<1];
int head[MAXN],ecnt;
int dis[MAXN][21];//MAXN点,15层 
int vis[MAXN][21];
int n,m,k;
void intt()
{
	clean(head,-1);
	ecnt=0;
}
void add(int u,int v,int w)
{
	edge[ecnt]=node(v,w,head[u]);
	head[u]=ecnt++;
}
/*---上面的是板子,不用动---*/
void djstr()
{
	clean(dis,0xf);//INF 
	dis[1][0]=0;//第i个点,第j层
	priority_queue<pair<int,pair<int,int> > > que;
	//优先队列,大的值优先,我们定义负的边可以 取负的最大,就是正的最小 
	//定义一个int,<int,int>的优先队列 表示,(距离,<当前点,当前层>) 
	que.push(make_pair(0,make_pair(1,0)));//第一个表示点,第二个表示层
	//距离为0的起点(第一个点,第0层) 入列 
	while(que.size())
	{
		while(vis[que.top().second.first][que.top().second.second]//当前点,当前层被走过了 
		&&que.size()>0)//队列中还有元素 
			que.pop();//取出 
		pair<int,int> p=que.top().second;//当前点,当前层 
		vis[p.first][p.second]=1;//标记 当前点,当前层 
		for(int i=head[p.first];i+1;i=edge[i].nxt)//当前点的连接表 
		{
			int will=edge[i].v;//目标点 
			//同层的查找 
			if(vis[will][p.second]==0//目标点,当前层没有标记过 
			&&dis[will][p.second]>dis[p.first][p.second]+edge[i].w)
			{//v点,当前层的值 比 上一点的那个值 大 (可以刷新) 
				dis[will][p.second]=dis[p.first][p.second]+edge[i].w;
				que.push(make_pair(-dis[will][p.second],make_pair(will,p.second)));
				//刷新后该点入列,(距离,<v点,同一层>) 
			}//前往可以去的下一层 
			if(p.second+1<=k&&vis[will][p.second+1]==0//该点的这一层没有走过 
			&&dis[will][p.second+1]>dis[p.first][p.second])//可以刷新 
			{
				dis[will][p.second+1]=dis[p.first][p.second];//刷新 
				que.push(make_pair(-dis[will][p.second+1],make_pair(will,p.second+1)));
				//刷新后 放入(到v的距离,<v点,下一层>) 
			}
		}
	}
}

int main()
{
	intt();
	scanf("%d%d%d",&n,&m,&k);
	int a,b;
	int l;
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d%d",&a,&b,&l);
		add(a,b,l);
		add(b,a,l);
	}
	djstr();
	printf("%d\n",dis[n][k]);//走到最后一层的目标点 
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/82352933