【HYSBZ - 2763 】飞行路线 (分层图最短路,最短路dp)

题干:

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

Input

数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。

第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)

接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)

 

Output

 

只有一行,包含一个整数,为最少花费。

Sample Input

5 6 1 0 4 0 1 5 1 2 5 2 3 5 3 4 5 2 3 3 0 2 100

Sample Output

8

Hint

 

对于30%的数据,2<=n<=50,1<=m<=300,k=0;

 

对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;

 

对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.

 

解题报告:

  注意一下数据范围,k=10,所以你需要乘以11,,,这题控制不好数据范围很容易就RE了、、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 10000 + 5;
const int MAXM = 50000 + 5;
struct edge {int to, ne;int w;
} e[MAXM*4*11];
struct node {int u, c;node() {}node(int u, int c) : u(u), c(c) {}bool operator < (const node &a) const {return c > a.c;}
};
int head[MAXN*11], tot;
int dis[MAXN*11];
bool vis[MAXN*11];
int n, m, k;
int st,ed;
void add(int u, int v, int w) {e[++tot].to = v;e[tot].ne = head[u];e[tot].w = w;head[u] = tot;
}void Dijkstra(int st) {memset(dis, INF, sizeof dis);dis[st] = 0;priority_queue<node> pq;pq.push(node(st, 0));while(!pq.empty()) {node cur = pq.top(); pq.pop();if(vis[cur.u]) continue;vis[cur.u] = 1;for(int i=head[cur.u]; i!=-1; i=e[i].ne) {int v = e[i].to;if(dis[v] > cur.c+e[i].w) {dis[v] = cur.c+e[i].w;pq.push(node(v,dis[v]));}}}
}
int main() 
{memset(head, -1, sizeof head);tot = 0;scanf("%d%d%d",&n,&m,&k);cin>>st>>ed;for(int i=1; i<=m; i++) {int u, v, w;scanf("%d%d%d",&u,&v,&w);for(int j = 0; j<=k; j++) {add(u+j*n,v+j*n,w);add(v+j*n,u+j*n,w);}for(int j = 0; j<k; j++) {add(u+j*n,v+(j+1)*n,0);add(v+j*n,u+(j+1)*n,0);}}Dijkstra(st);int ans = INF;for(int j = 0; j<=k; j++) {//不用~用k次,一共k+1个图 ans = min(ans,dis[j*n+ed]); }printf("%d\n",ans);return 0 ;
}

建立分层图的代码:

for(int i=1; i<=m; ++i) {scanf("%d%d%d",&a,&b,&c);for(int j=0; j<=k; ++j) {add(a+j*n,b+j*n,c);add(b+j*n,a+j*n,c);if(j<k) {add(a+j*n,b+(j+1)*n,0);add(b+j*n,a+(j+1)*n,0);}}
}

我们有k次免费坐飞机的机会,那么就有k+1个图 
我们只要保证我们每一次创建的图满足i*n+a(当前位置的标号)。

 

20190508更新:最短路dp的做法。

还WA了一发,,因为写着写着就忘了是双向边了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int dis[10005][12];
bool vis[10005][12];
int head[MAX],tot;
int n,m,k,st,ed;
struct Edge {int to,ne,w;
} e[MAX];
void add(int u,int v,int w) {e[++tot].to = v;e[tot].w=w;e[tot].ne=head[u];head[u] = tot;
}
struct Point {int pos,ci,c;Point(int pos=0,int ci=0,int c=0):pos(pos),ci(ci),c(c){}bool operator<(const Point b)const {return c > b.c;} 
};
void Dijkstra() {priority_queue<Point> pq;for(int i = 1; i<=n; i++) {for(int j = 0; j<=k; j++) dis[i][j]=INF,vis[i][j]=0;}dis[st][0]=0;pq.push(Point(st,0,0));while(pq.size()) {Point cur = pq.top();pq.pop();if(cur.pos == ed) break;if(vis[cur.pos][cur.ci]) continue;vis[cur.pos][cur.ci] = 1;for(int i = head[cur.pos]; ~i; i = e[i].ne) {int v = e[i].to;if(vis[v][cur.ci] == 0) {if(dis[v][cur.ci] > dis[cur.pos][cur.ci] + e[i].w) {dis[v][cur.ci] = dis[cur.pos][cur.ci] + e[i].w;pq.push(Point(v,cur.ci,dis[v][cur.ci]));}}int tmpci = cur.ci+1;if(tmpci > k) continue;if(vis[v][tmpci] == 0) {if(dis[v][tmpci] > dis[cur.pos][cur.ci]) {dis[v][tmpci] = dis[cur.pos][cur.ci];pq.push(Point(v,tmpci,dis[v][tmpci]));}}}				}
}
int main()
{cin>>n>>m>>k;cin>>st>>ed;st++,ed++;for(int i = 1; i<=n+1; i++) head[i] = -1;for(int a,b,c,i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&c);a++,b++;add(a,b,c);add(b,a,c);}Dijkstra();int ans = INF;for(int i = 0; i<=k; i++) {ans = min(ans,dis[ed][i]);}printf("%d\n",ans);return 0 ;
}
//21:20 - 21:39

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440823.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 命令行 编译 jar文件_用命令行编译java并生成可执行的jar包

如果想用java编写一个可视化小程序&#xff0c;碰巧手头没有IDE的话&#xff0c;可以用命令行来完成编译、打包等工作。拿自己编写的“java记事本”为例&#xff0c;介绍一下这个过程&#xff1a;1.编写源代码。编写源文件&#xff1a;NotePad.java并保存&#xff0c;例如&…

【牛客 - 369A】小D的剧场(线性dp)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/369/A 来源&#xff1a;牛客网 题目描述 "我明白。" 作为这命运剧场永远的观众&#xff0c;小D一直注视着这片星光璀璨的舞台&#xff0c;舞台上&#xff0c;少女们的身姿演绎出了一幕幕…

java list遍历添加元素_java遍历List过程中添加和删除元素的问题

遍历元素最常见的三种方法&#xff1a;//第三种遍历【利用迭代器】private static void loopList3(List strList) {Iterator itr strList.iterator();while (itr.hasNext()){String tmp itr.next();if("000".equals(tmp)){itr.remove();}else{System.out.println(t…

【蓝桥杯官网试题 - 算法提高 】求最大值 (dp,0-1背包)

题干&#xff1a; 问题描述 给n个有序整数对ai bi&#xff0c;你需要选择一些整数对 使得所有你选定的数的aibi的和最大。并且要求你选定的数对的ai之和非负&#xff0c;bi之和非负。 输入格式 输入的第一行为n&#xff0c;数对的个数   以下n行每行两个整数 ai bi 输出格…

java button 圆角_UIButton具有渐变边框和圆角

我想要的是一个自定义UIButton&#xff0c;它有一个渐变边框(只是边框是渐变)和圆角 . 我几乎到了我想去的地方&#xff0c;但是角落有问题 . 这是我目前拥有的&#xff1a;这是我的代码&#xff1a;override func viewDidLoad() {super.viewDidLoad()let gradient CAGradient…

【牛客 - 368B】选点(dfs序,LIS 或 dfs序 + 树状数组 + 离散化,树状数组求LIS的方法)

题干&#xff1a; 有一棵n个节点的二叉树&#xff0c;1为根节点&#xff0c;每个节点有一个值wi。现在要选出尽量多的点。 对于任意一棵子树&#xff0c;都要满足&#xff1a; 如果选了根节点的话&#xff0c;在这棵子树内选的其他的点都要比根节点的值大&#xff1b; 如…

java xsd 解析 xml文件_Java针对XSD文件验证XML文件的最佳方法是什么?

小编典典Java运行时库支持验证。上次我检查的是幕后的Apache Xerces解析器。你可能应该使用javax.xml.validation.Validator。import javax.xml.XMLConstants;import javax.xml.transform.Source;import javax.xml.transform.stream.StreamSource;import javax.xml.validation.…

*【CodeForces - 859C 】Pie Rules (博弈dp,时光倒流)

题干&#xff1a; You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices …

【牛客 - 318F】关于我转生变成史莱姆这档事(二分,搜索)

题干&#xff1a; 有一天&#xff0c;利姆鲁在这个世界最重要的人静被魔王带走&#xff0c;并将其困在一个n*n的迷宫内的某一处&#xff0c;迷宫的每个格子都可能有一只魔物&#xff0c;魔物的攻击力为a[i][j]&#xff0c;因而利姆鲁只有当攻击力大于等于a[i][j]才能通过这个方…

java获取xlsx某列数据_Java读取Excel指定列的数据详细教程和注意事项

本文使用jxl.jar工具类库实现读取Excel中指定列的数据。jxl.jar是通过java操作excel表格的工具类库&#xff0c;是由java语言开发而成的。这套API是纯Java的&#xff0c;并不依赖Windows系统&#xff0c;即使运行在Linux下&#xff0c;它同样能够正确的处理Excel文件。支持Exce…

【CodeForces - 988C 】Equal Sums (思维,STLmap,STLset,tricks)

题干&#xff1a; You are given kk sequences of integers. The length of the ii-th sequence equals to nini. You have to choose exactly two sequences ii and jj (i≠ji≠j) such that you can remove exactly one element in each of them in such a way that the su…

java quartz 数据库_SpringBoot+Quartz+数据库存储

Spring整合Quartza、quartz调度框架是有内置表的进入quartz的官网http://www.quartz-scheduler.org/&#xff0c;点击Downloads&#xff0c;下载后在目录\docs\dbTables下有常用数据库创建quartz表的脚本&#xff0c;例如&#xff1a;“tables_mysql.sql”table_mysql.sqltable…

【CodeForces - 1062C】Banh-mi (贪心,数学,找规律,快速幂)

题干&#xff1a; JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into nn pa…

【牛客 - 练习】约数个数的和(数论,数学)

题干&#xff1a; 给个n&#xff0c;求1到n的所有数的约数个数的和~ 输入描述: 第一行一个正整数n 输出描述: 输出一个整数&#xff0c;表示答案 示例1 输入 复制 3 输出 复制 5 说明 样例解释&#xff1a; 1有1个约数1 2有2个约数1,2 3有2个约数1,3 备注: n…

mysql json 创建索引_MySQL · 最佳实践 · 如何索引JSON字段

概述MySQL从5.7.8起开始支持JSON字段&#xff0c;这极大的丰富了MySQL的数据类型。也方便了广大开发人员。但MySQL并没有提供对JSON对象中的字段进行索引的功能&#xff0c;至少没有直接对其字段进行索引的方法。本文将介绍利用MySQL 5.7中的虚拟字段的功能来对JSON对象中的字段…

mysql链路跟踪工具_EasySwoole利用链路追踪组件制作甩锅工具

前言最近前端老是反馈API调用异常&#xff0c;说请求成功但是没有数据返回&#xff01;我写的代码怎么可能有bug&#xff0c;肯定是前端调用的方式不对&#xff01;经过一番套鼓&#xff0c;直接把请求参数和响应内容打印到控制台&#xff0c;果然不出我所料&#xff0c;请求缺…

【CCFCSP- 201312-4】有趣的数(线性dp)

题干&#xff1a; 试题编号&#xff1a;201312-4试题名称&#xff1a;有趣的数时间限制&#xff1a;1.0s内存限制&#xff1a;256.0MB问题描述&#xff1a; 问题描述   我们把一个数称为有趣的&#xff0c;当且仅当&#xff1a;   1. 它的数字只包含0, 1, 2, 3&#xff0c…

java selector 源码_Java NIO核心组件-Selector和Channel

昨天我们介绍了一下SelectorProvider和IO multiplexing.特别是IO multiplexing中的epoll系统调用,是Linux版本的Java的NIO的核心实现.那今天我们就来介绍一下, Java NIO中的核心组件, Selector和Channel.这两个组件,对于熟悉Java OIO,而不熟悉Java NIO的朋友来说,理解其作用是极…

【HDU - 1542】Atlantis (线段树,扫描线)

题干&#xff1a; There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend…

python 底层原理processpoolexecutor_python 多进程并行编程 ProcessPoolExecutor的实现

使用 ProcessPoolExecutorfrom concurrent.futures import ProcessPoolExecutor, as_completedimport random斐波那契数列当 n 大于 30 时抛出异常def fib(n):if n > 30:raise Exception(can not > 30, now %s % n)if n < 2:return 1return fib(n-1) fib(n-2)准备数组…