Codeforces 938D Buy a Ticket

Buy a Ticket

题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的。

题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了,  dis数组存的是, 当前情况下的最小花费是多少。

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<queue>
 5 #include<vector>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<iomanip>
 9 #include<cstdio>
10 #define LL long long
11 #define ULL unsigned LL
12 #define lson l,m,rt<<1
13 #define rson m+1,r,rt<<1|1
14 #define fi first
15 #define se second
16 using namespace std;
17 typedef pair<LL, int> pll;
18 const int N = 2e5+5;
19 LL dis[N];
20 int head[N];
21 struct Node{
22     int to;
23     int nt;
24     LL ct;
25 }e[N<<1];
26 priority_queue<pll, vector<pll>, greater<pll> > q;
27 void dijkstra(){
28     while(!q.empty()){
29         int u = q.top().se;
30         LL w = q.top().fi;
31         q.pop();
32         if(dis[u] != w) continue;
33         for(int i = head[u]; ~i; i = e[i].nt){
34             int v = e[i].to;
35             if(dis[v] > dis[u] + e[i].ct){
36                 dis[v] = dis[u] + e[i].ct;
37                 q.push(pll(dis[v],v));
38             }
39         }
40     }
41 }
42 int tot = 0;
43 void add(int u, int v, LL w){
44     e[tot].ct = w;
45     e[tot].to = v;
46     e[tot].nt = head[u];
47     head[u] = tot++;
48 }
49 int main(){
50     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
51     memset(head, -1, sizeof(head));
52     int n, m;
53     cin >> n >> m;
54     int u, v;
55     LL ct;
56     for(int i = 1; i <= m; i++){
57         cin >> u >> v >> ct;
58         add(u,v,ct*2);
59         add(v,u,ct*2);
60     }
61     for(int i = 1; i <= n; i++){
62         cin >> ct;
63         q.push(pll(ct,i));
64         dis[i] = ct;
65     }
66     dijkstra();
67     for(int i = 1; i < n; i++){
68         cout << dis[i] << ‘ ‘;
69     }
70     cout << dis[n] << endl;
71     return 0;
72 }

原文地址:https://www.cnblogs.com/MingSD/p/8494955.html

时间: 2024-10-05 06:44:10

Codeforces 938D Buy a Ticket的相关文章

Codeforces 938D Buy a Ticket (转化建图 + 最短路)

题目链接  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

Codeforces 938D Buy a Ticket 【spfa优化】

用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是min(2*dis[i][j]+a[j]} ),那么每个点到s的距离就是若干条边边权的二倍加上某个点的点权,并且这个组合是最小的.证毕. #include<iostream> #include<cstdio> #include<queue> using namespace

最短路 || Codeforces 938D Buy a Ticket

题意:从城市u到v(双向)要花w钱,每个城市看演唱会要花不同的门票钱,求每个城市的人要看一场演唱会花费最少多少(可以在这个城市看,也可以坐车到别的城市看,然后再坐车回来) 思路:本来以为是多源..实际上是单源 考虑dij的松弛操作,是每次取队列里值最小的点u(队首),看它能拓展到的点v,如果经过u到v的代价比当前到v的代价低,那么就更新v 这里也同理,只不过代价是路程*2加上在v看演唱会的钱 嗯..神奇的dij #include <iostream> #include <cstdio&g

Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)

题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的最短路,但是从点的个数上就知道这题不可以用floyd算法,其实多元最短路可以用dijkstra算.@.@!把所有的点的权值和点放到结构体里面,放入优先队列,其实这样就能保证每次拓展到的点就是这个点的最短路(因为是优先队列,保证拓展到的点这时候的值是最小的),其实就是这个点想通就很简单. 1 #inc

Codeforces 938 D. Buy a Ticket

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 the Ticket DP +大数

Buy the Ticket 题目抽象:有m个手持50元的人,n个手持100元的人,售票处没有准备钱.问有多少种不同的方案使得购票不中断.每个人是不同的个体. 分析:简单DP题.画个格子,那么选取的点不越过对角线y = x.  dp[i][j]表示i个100元的人,j个手持50元的人的方案数.     i<=j dp[i][j] = dp[i-1][j]+dp[i][j-1];  i>j  dp[i][j] = 0;  如果对某些点有限制,那么在递推是加条件判断. ans = dp[n][m]

hdu Buy the Ticket

1 import java.math.BigInteger; 2 import java.util.*; 3 public class Main { 4 public static void main(String []args) 5 { 6 Scanner cin=new Scanner(System.in); 7 int n,m,i; 8 int t1=0; 9 while(cin.hasNextBigInteger()) 10 { 11 t1++; 12 m=cin.nextInt();

Buy the Ticket(卡特兰数+递推高精度)

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{HDU1133}

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