【洛谷P4568】飞行路线[JLOI2011]
今天介绍一种最短路情景下的板子题!
主要情景大概是这样:
给一个无向图,从某节点到某节点,中间可以对k条边进行处理,减费或者零费
求最短路
我们有两种解法,先看第一种:
在最短路里跑DP
P1948 [USACO08JAN] Telephone Lines S
题目描述
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.
There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.
The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.
As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.
Determine the minimum amount that Farmer John must pay.
多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着 \(1\le N\le1000\) 根据 \(1\cdots N\) 顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有 \(1\le p\le10000\) 对电话杆可以拉电话线。其他的由于地震使得无法连接。
第i对电线杆的两个端点分别是 \(a_i\) ,\(b_i\),它们的距离为 \(1\le l_i\le1000000\)。数据中每对 \((a_i,b_i)\) 只出现一次。编号为 \(1\) 的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号 \(N\) 的电话线杆上。也就是说,笨笨的任务仅仅是找一条将 \(1\) 号和 \(N\) 号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。
电信公司决定支援灾区免费为此市连接 \(k\) 对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过 \(k\) 对,那么支出为 \(0\)。
请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?
输入格式
输入文件的第一行包含三个数字 \(n,p,k\)。
第二行到第 \(p+1\) 行,每行分别都为三个整数 \(a_i,b_i,l_i\)。
输出格式
一个整数,表示该项工程的最小支出,如果不可能完成则输出 -1。
输入输出样例 #1
输入 #1
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
输出 #1
4
Solution
设dp[i][j]为到i点时用了j个免费的最短路
然后在最短路算法里更新即可
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,p,k,tot;
int u,v,w;
int head[20005],nex[20005],edge[20005],ver[20005];
int dp[1005][20005];
int vis[1005][20005];
const int INF=0x3f;
queue<pair<int,int>>q;
void add(int x,int y,int z){
ver[++tot]=y;
edge[tot]=z;
nex[tot]=head[x];
head[x]=tot;
}
void spfa(){
memset(dp,0x3f,sizeof(dp));
vis[1][0]=1;dp[1][0]=0;
q.push(make_pair(1,0));
while(!q.empty()){
int x=q.front().first;
int tem=q.front().second;
q.pop();
vis[x][tem]=0;
for(int i=head[x];i;i=nex[i]){
int y=ver[i],z=edge[i];
if(dp[y][tem]>max(dp[x][tem],z)){ //不免费这条线路
dp[y][tem]=max(dp[x][tem],z);
if(!vis[y][tem]){
q.push(make_pair(y,tem));
vis[y][tem]=1;
}
}
if(dp[y][tem+1]>dp[x][tem]&&tem<=k-1){//免费这条线路
dp[y][tem+1]=dp[x][tem];
if(!vis[y][tem+1]){
q.push(make_pair(y,tem+1));
vis[y][tem+1]=1;
}
}
}
}
}
int main(){
scanf("%d%d%d",&n,&p,&k);
for(int i=1;i<=p;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
spfa();
if(dp[n][k]==1061109567){
cout<<-1;
system("pause");
return 0;
}
else{
cout<<dp[n][k];
}
system("pause");
return 0;
}
方法二:分层图
什么是分层图?顾名思义,就是把一个图转化成立体模型,也就是(k+1)层图叠成的立方体
可以从上层图到达下层图 且费用为0,反之则不行
单层图内的分布跟原图相同
这个的唯一难点就是 呃 数组的大小要注意
下面我们看一道例题:
P4568 [JLOI2011] 飞行路线
题目描述
Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在 \(n\) 个城市设有业务,设这些城市分别标记为 \(0\) 到 \(n-1\),一共有 \(m\) 种航线,每种航线连接两个城市,并且航线有一定的价格。
Alice 和 Bob 现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多 \(k\) 种航线上搭乘飞机。那么 Alice 和 Bob 这次出行最少花费多少?
输入格式
第一行三个整数 \(n,m,k\),分别表示城市数,航线数和免费乘坐次数。
接下来一行两个整数 \(s,t\),分别表示他们出行的起点城市编号和终点城市编号。
接下来 \(m\) 行,每行三个整数 \(a,b,c\),表示存在一种航线,能从城市 \(a\) 到达城市 \(b\),或从城市 \(b\) 到达城市 \(a\),价格为 \(c\)。
输出格式
输出一行一个整数,为最少花费。
输入输出样例 #1
输入 #1
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
输出 #1
8
说明/提示
数据规模与约定
对于 \(30\%\) 的数据,\(2 \le n \le 50\),\(1 \le m \le 300\),\(k=0\)。
对于 \(50\%\) 的数据,\(2 \le n \le 600\),\(1 \le m \le 6\times10^3\),\(0 \le k \le 1\)。
对于 \(100\%\) 的数据,\(2 \le n \le 10^4\),\(1 \le m \le 5\times 10^4\),\(0 \le k \le 10\),\(0\le s,t,a,b < n\),\(a\ne b\),\(0\le c\le 10^3\)。
另外存在一组 hack 数据。
Solution
就像我上面说的 只有建图有差别 其他没差别
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,k,tot,s,t;
int x,y,z;
int head[2200005],nex[2200005],edge[2200005],ver[2200005];
int vis[2200005],dis[2200005];
int ans=1e9;
priority_queue<pair<int,int>>q;
void add(int x,int y,int z){
ver[++tot]=y;
edge[tot]=z;
nex[tot]=head[x];
head[x]=tot;
}
void dijkstra(int sx){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[sx]=0;
q.push(make_pair(0,sx));
while(!q.empty()){
int x=q.top().second;q.pop();
if(vis[x]) continue;
vis[x]=1;
for(int i=head[x];i;i=nex[i]){
int y=ver[i],z=edge[i];
if(dis[y]>dis[x]+z){
dis[y]=dis[x]+z;
q.push(make_pair(-dis[y],y));
}
}
}
}
int main(){
scanf("%d%d%d",&n,&m,&k);
scanf("%d%d",&s,&t);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
for(int j=1;j<=k;j++){
add(x+(j-1)*n,y+j*n,0);add(y+(j-1)*n,x+j*n,0);
add(x+j*n,y+j*n,z);add(y+j*n,x+j*n,z);
}
}
dijkstra(s);
for(int i=0;i<=k;i++){
ans=min(ans,dis[t+i*n]);
}
printf("%d",ans);//注意 由于一条路可能走了不到k条边 所以应该遍历一下dis数组求出ans
system("pause");
return 0;
}

浙公网安备 33010602011771号