最短路 || Codeforces 938D Buy a Ticket】的更多相关文章

题意:从城市u到v(双向)要花w钱,每个城市看演唱会要花不同的门票钱,求每个城市的人要看一场演唱会花费最少多少(可以在这个城市看,也可以坐车到别的城市看,然后再坐车回来) 思路:本来以为是多源..实际上是单源 考虑dij的松弛操作,是每次取队列里值最小的点u(队首),看它能拓展到的点v,如果经过u到v的代价比当前到v的代价低,那么就更新v 这里也同理,只不过代价是路程*2加上在v看演唱会的钱 嗯..神奇的dij #include <iostream> #include <cstdio&g…
题目链接  Buy a Ticket 题意   给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$n+1$, 对于每一条无向边$(x, y, z)$,$x$向$y$连一条长度为$2z$的边,反之亦然. 对于每个$a_{i}$, 从$i$到$n+1$连一条长度为$a_{i}$的边,反之亦然. 然后跑一边最短路即可. #include <bits/stdc++.h> using namespace…
Buy a Ticket 题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的. 题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了,  dis数组存的是, 当前情况下的最小花费是多少. 代码: #include<iostream> #include<cstring> #include<string> #include<queue> #include<vector> #includ…
<题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值=所看演唱会的价值+该城市的人去那个城市看演唱会的往返距离之和). 解题分析:比较好的一道最短路题,主要考察建图能力.我们不妨建立一个虚拟源点,然后该源点向所有的边都连上一条无向边,边权为对应点的点权.然后所有的点之间通过m条边连接,只不过边权设为2倍原边权.最后就是从源点跑一遍最短路,就能得到每个城市的人能够…
用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是min(2*dis[i][j]+a[j]} ),那么每个点到s的距离就是若干条边边权的二倍加上某个点的点权,并且这个组合是最小的.证毕. #include<iostream> #include<cstdio> #include<queue> using namespace…
题目大意 流行乐队"Flayer"将在\(n\)个城市开演唱会,这\(n\)个城市的人都想去听演唱会,每个城市的票价不同,于是这些人就想是否能去其他城市听演唱会更便宜,但是去其他的城市也需要路费. 输入格式 第一行包含两个整数\(n\)和\(m\). 接下来\(m\)行,每行三个数\(u.v.w\)表示\(u\)城市到\(v\)城市要\(w\)元. 接下来\(n\)个数,表示每个城市的票价\(a\). 输出格式 输出n个数字,表示对应的人到别的城市参加演唱会再返程的最小费用,当然也可以…
题目 Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well. There are n cities in Berland. People can travel between cities using two-di…
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的最短路,但是从点的个数上就知道这题不可以用floyd算法,其实多元最短路可以用dijkstra算.@.@!把所有的点的权值和点放到结构体里面,放入优先队列,其实这样就能保证每次拓展到的点就是这个点的最短路(因为是优先队列,保证拓展到的点这时候的值是最小的),其实就是这个点想通就很简单. #inclu…
D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tou…
Buy A Ticket 题目大意 每个点有一个点权,每个边有一个边权,求对于每个点u的\(min(2*d(u,v)+val[v])\)(v可以等于u) solution 想到了之前的虚点,方便统计终点的权值,将所有点和虚点建边,边权不变,这样只需要求虚点到其他点的最短路即可,就将多源最短路问题转换成了单源最短路 #include <cstdio> #include <cstring> #include <queue> using namespace std; stru…
Buy the TicketTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6517 Accepted Submission(s): 2720 Problem DescriptionThe "Harry Potter and the Goblet of Fire" will be on show in the next few day…
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you? Suppose the cinema only has one tic…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4726    Accepted Submission(s): 1993 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1886 Accepted Submission(s): 832   Problem Description The \\\\\\\"Harry Potter and the Goblet of Fire\\\\\\\" will be on show i…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5651    Accepted Submission(s): 2357 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3614    Accepted Submission(s): 1522 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4185    Accepted Submission(s): 1759 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next…
题目链接:https://vjudge.net/problem/HDU-1133 Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7427    Accepted Submission(s): 3105 Problem Description The "Harry Potter and the Goblet…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7152    Accepted Submission(s): 2998 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the nex…
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8838    Accepted Submission(s): 3684 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next…
大意: n节点无向图, 点$i$到点$j$的花费为$2dis(i,j)+a[j]$, 对于每个点, 求最少花费. 每条边权翻倍, 源点S向所有点$i$连边, 权为$a[i]$, 答案就为$S$到每个点的最短路距离. #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set>…
题目 Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well. There are n cities in Berland. People can travel between cities using two-di…
题意 给定一张无向图,对每个点$i\in S$求$\min_{j\in S} {2\times d(i,j)+a_j}$ 考虑多源多汇最短路会超时,换个角度考虑每个$j$,如果$j=i$,那么答案为$a_i$,如果有更优的方案,那么为$i$到$j$的一条路径加上$a_j$,将这个过程看成两条路径,并且将$a_j$独立为一条路径,就得到最短路算法中的松弛操作,可以建立一个超级源点,连向各点,边权为$a_i$,那么从源点跑一遍最短路之后就是每个点所求答案 时间复杂度$O(n\log n)$ 代码 #…
题目传送门 /* 最短路: 不仅扫描边,还要扫描点:点有两种情况,一种刚好在中点,即从u,v都一样,那么最后/2 还有一种是从u,v不一样,两种的距离都是l 模板错了,逗了好久:( */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <queue> using namespace…
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找出 \(i\) 号点到 \(j\) 号点的最短路径.则该最短路径只有两种可能: \(i\) 号点直接到达 \(j\) 号点的路径中产生最短路径 \(i\) 号点经过一些中间点到达 \(j\) 号点的路径中产生最短路径 我们添加一个点 \(k\),使得 \(i\) 号点到 \(j\) 号点再添加后产生…
考试T1,建一个反图跑一个最短路就好了~ code: #include <bits/stdc++.h> #define ll long long #define N 200002 #define M 600009 #define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout) using namespace std; int n,m,…
题目连接 题意: 没个位置有一个点权,每个边有一个边权,求对于每个点u的min(2*d(u,v)+val[v])(v可以等于u) 分析: 我们想这样一个问题,从u到v的边权*2再加一个点权就完了,我们能不能把点权也变成边权,可以,直接和0连接就好了,这是从u到0的最短路(当然原先的边权要*2)就是要求的值. 当然,也可以直接类似dij的贪心思想,把每个点的dis赋值为点权push进去然后更新就行了.其实是类似的算法. 代码: #include <cstdio> #include <cst…
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4981    Accepted Submission(s): 2085 Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a…
首先,记50的为0,100的为1. 当m=4,n=3时,其中的非法序列有0110010; 从不合法的1后面开始,0->1,1->0,得到序列式0111101 也就是说,非法序列变为了n-1个0,m+1个1. 总的数目=C(m+n,n),非法的=C(m+n,m+1) 符合数目=(C(m+n,n)-C(m+n,m+1))*m!*n!; 化简得:(m+n)!*(m+1-n)/(m+1). Java代码: import java.io.*; import java.math.*; import jav…
题意:你手中有10元的钱,还有一个r元的零钱,要买一个价格为k的物品,但是你要求不找零钱,求最少要买多少物品. 析:直接暴力,最多买10个物品就够了1-10. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #includ…