BZOJ 2763: [JLOI2011]飞行路线 spfa dp

题目链接:

http://www.lydsy.com/JudgeOnline/problem.php?id=2763

题解:

d[x][kk]表示从s到x用了kk次免费机会的最少花费。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#define mp make_pair
#define X first
#define Y second
using namespace std;

const int maxn = 10000;

int n, m, k;
vector<pair<int, int> > G[maxn];

int d[maxn][22];
bool inq[maxn][22];
void spfa(int s) {
	memset(d, 0x7f, sizeof(d));
	memset(inq, 0, sizeof(inq));
	queue<pair<int,int> > Q;
	d[s][0] = 0, inq[s][0] = 1, Q.push(mp(s,0));
	while (!Q.empty()) {
		int u = Q.front().X,kk=Q.front().Y;
		Q.pop();
		inq[u][kk] = 0;
		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i].X, w = G[u][i].Y;
			if (d[v][kk]>d[u][kk] + w) {
				d[v][kk] = d[u][kk] + w;
				if (!inq[v][kk]) {
					inq[v][kk] = 1, Q.push(mp(v, kk));
				}
			}
			if (kk + 1 <= k&&d[v][kk + 1] > d[u][kk]) {
				d[v][kk + 1] = d[u][kk];
				if (!inq[v][kk + 1]) {
					inq[v][kk + 1] = 1, Q.push(mp(v, kk + 1));
				}
			}
		}
	}
}

void init() {
	for (int i = 0; i < n; i++) G[i].clear();
}

int main() {
	while (scanf("%d%d%d", &n, &m, &k) == 3) {
		init();
		int s, t;
		scanf("%d%d", &s, &t);
		for (int i = 0; i < m; i++) {
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			G[u].push_back(mp(v, w));
			G[v].push_back(mp(u, w));
		}
		spfa(s);
		printf("%d\n", d[t][k]);
	}
	return 0;
}

  

时间: 2024-07-29 15:34:19

BZOJ 2763: [JLOI2011]飞行路线 spfa dp的相关文章

Bzoj 2763: [JLOI2011]飞行路线 拆点,分层图,最短路,SPFA

2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1694  Solved: 635[Submit][Status][Discuss] Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格.Alice和Bob现在要从一个城市沿着航线到达另一个城市

Bzoj 2763: [JLOI2011]飞行路线 dijkstra,堆,最短路,分层图

2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1728  Solved: 649[Submit][Status][Discuss] Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格.Alice和Bob现在要从一个城市沿着航线到达另一个城市

分层图+最短路算法 BZOJ 2763: [JLOI2011]飞行路线

2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格.Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机.航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机.那么Al

BZOJ 2763: [JLOI2011]飞行路线 【SPFA】

Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格.Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机.航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机.那么Alice和Bob这次出行最少花费多少?Input数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数.第二行有两个整数,s,t,分

bzoj 2763: [JLOI2011]飞行路线【分层图+spfa】

为什么早年的题总是从0开始标号啊--又zz了一次WA 分层图的题只有这一个套路吧,建分层图,然后优化时间是分层跑spfa然后层与层之间单独跑即可 #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; const int N=50005,inf=1e9; int n,m,k,s,t,h[N],cnt,dis[N],d[N]; bool

bzoj 2763 [JLOI2011]飞行路线——分层图

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算优秀. #include<iostream> #include<cstdio> #include<cstring> #include<queue> #define ll long long using namespace std; const int N=1e4

bzoj 2763: [JLOI2011]飞行路线

/* 这做法好像有个很高大上的名字叫做分层图. 其实就是按照题目的意思重新建图 节点有n个变成n*k个 每个表示节点的编号和剩下能用的k值 然后...卡spfa 需要堆优化的dij */ #include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<vector> #define pa pair<int,int> #define mk ma

bzoj 2763: [JLOI2011]飞行路线 分层图

题目链接 n个点m条路, 每条路有权值,  给出起点和终点, 求一条路使得权值最小.可以使路过的路中, k条路的权值忽略. 其实就是多一维, 具体看代码 #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a)

[BZOJ 2763][JLOI 2011] 飞行路线

2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3203  Solved: 1223[Submit][Status][Discuss] Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格.Alice和Bob现在要从一个城市沿着航线到达另一个城