Educational Codeforces Round 38 (Rated for Div. 2) ----D

D. Buy a Ticket

问题转换为对于每一个点x,求出一个点y,使得xy的最短路2倍+在y举办的费用最小。

考虑建一个超级源点,向每一个点连一条费用为其举办所需费用,并且原图中的边权值*2,跑一遍最短路,每一个点到超级源点的最短路即为答案。

卡spfa,请用dijkstra。

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 const int inf=2e5+10;
 5 int n,m;
 6 int tot,fi[inf],to[inf<<2],nxt[inf<<2];
 7 int s;
 8 LL cost[inf<<2];
 9 void link(int x,int y,LL z){
10     to[++tot]=y;nxt[tot]=fi[x];fi[x]=tot;cost[tot]=z;
11 }
12 LL dis[inf];
13 struct dijk{
14     int id;
15     LL val;
16     bool operator < (const dijk &o)const{
17         return val>o.val;
18     }
19 };
20 priority_queue<dijk>S;
21 bool vis[inf];
22 void dijkstra(){
23     memset(dis,0x3f,sizeof(dis));
24     dis[s]=0;
25     S.push((dijk){s,0});
26     while(!S.empty()){
27         dijk u=S.top();
28         S.pop();
29         if(vis[u.id])continue;
30         vis[u.id]=1;
31         for(int i=fi[u.id];i;i=nxt[i]){
32             if(dis[to[i]]>dis[u.id]+cost[i]){
33                 dis[to[i]]=dis[u.id]+cost[i];
34                 S.push((dijk){to[i],dis[to[i]]});
35             }
36         }
37     }
38 }
39 int main()
40 {
41     scanf("%d%d",&n,&m);
42     for(int i=1;i<=m;i++){
43         int x,y;
44         LL z;
45         scanf("%d%d%lld",&x,&y,&z);
46         link(x,y,z<<1);link(y,x,z<<1);
47     }
48     for(int i=1;i<=n;i++){
49         LL z;
50         scanf("%lld",&z);
51         link(s,i,z);
52     }
53     dijkstra();
54     for(int i=1;i<=n;i++)printf("%lld ",dis[i]);
55     puts("");
56     return 0;
57 }

真的超级感谢这道题目啊,之前写的dijkstra一直是不对的,以至于我一直对其复杂度怀疑(事实上确实是不对的),甚至觉得他和不优化的dijkstra不像是一种算法了,普通的dijkstra每一个点只会被拿来更新一次,而我之前写的优化后的dijkstra是没有标记数组的,也就是说每一个点可能被访问多次,复杂度就不是nlog+m了。而且之前从来没有因为这个被卡过,这次体会到了。

原文地址:https://www.cnblogs.com/hyghb/p/8452797.html

时间: 2024-10-05 02:50:21

Educational Codeforces Round 38 (Rated for Div. 2) ----D的相关文章

Educational Codeforces Round 38 (Rated for Div. 2) ----C

C. Constructing Tests 经过简单的分析之后,我们可以发现,对于x,我们要求的就是一组n,m满足n^2-(n/m)^2=x. 有两种求法. 第一种: 观察n的极限范围是多少,我们发现,对于相同的n,m越大,该式值就越大,x的范围是1e9,为了得到n最大是多少,我们尽可能的减小m的值,我们发现m=1时式子没有什么意义,特判即可,当m=2时,我们发现左边为3/4*n^2,也就是说n的范围是sqrt(x)级别的,枚举n判断是否存在m即可. 第二种:原式可以化为(n-(n/m))*(n

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html