cf938D
题意简述:n个点m条边的无向图,有点权,有边权, 对于每一个点计算,d(i,j)表示点i到点j的最短路
题解:边权扩大二倍,建立源点,然后源点向每一个点x连接一条权值为a[x]的边,然后跑最短路即可
- #include<bits/stdc++.h>
- #define forn(i, n) for (int i = 0; i < int(n); i++)
- #define fore(i, s, t) for (int i = s; i < (int)t; i++)
- #define fi first
- #define se second
- #define all(x) x.begin(),x.end()
- #define pf2(x,y) printf("%d %d\n",x,y)
- #define pf(x) printf("%d\n",x)
- #define each(x) for(auto it:x) cout<<it<<endl;
- #define pi pair<int,int>
- using namespace std;
- char inline nc(){
- static char buf[100000],*p1=buf,*p2=buf;
- return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
- }
- template <typename T>
- bool rd(T& v){
- static char ch;
- while(ch!=EOF&&!isdigit(ch)) ch=nc();
- if(ch==EOF) return false;
- for(v=0;isdigit(ch);ch=nc())
- v=v*10+ch-'0';
- return true;
- }
- template <typename T>
- void o(T p){
- static int stk[70],tp;
- if(p==0) {
- putchar('0');return ;
- }
- if(p<0) {
- p=-p;putchar('-');
- }
- while(p) stk[++tp]=p%10,p/=10;
- while(tp) putchar(stk[tp--]+'0');
- }
- typedef long long ll;
- const int maxn=2e5+5;
- const int maxm=6e5+5;
- const int inf=1e9;
- int head[maxn],ver[maxm],nex[maxm],tot;
- ll wi[maxm];
- int n,m;
- void inline AddEdge(int x,int y,ll z){
- ver[++tot]=y,wi[tot]=z,nex[tot]=head[x],head[x]=tot;
- }
- ll dis[maxn];
- bool vis[maxn];
- void dijkstra(){
- memset(dis,0x3f,sizeof(dis));
- dis[n+1]=0;
- multiset<pair<ll,int>> q;
- q.insert({0,n+1});
- while(q.size()){
- int x=q.begin()->se;q.erase(q.begin());
- if(vis[x]) continue;
- vis[x]=1;
- for(int i=head[x];i;i=nex[i]){
- int y=ver[i];
- if(dis[y]>dis[x]+wi[i]){
- dis[y]=dis[x]+wi[i];
- q.insert({dis[y],y});
- }
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin>>n>>m;
- for(int i=0;i<m;i++){
- int x,y;ll z;
- cin>>x>>y>>z;
- AddEdge(x,y,2*z);
- AddEdge(y,x,2*z);
- }
- for(int i=0;i<n;i++){
- ll x;
- cin>>x;
- AddEdge(n+1,i+1,x);
- }
- dijkstra();
- for(int i=1;i<=n;i++)
- cout<<dis[i]<<' ';
- cout<<"\n";
- }
cf938D的更多相关文章
- CF938D Buy a Ticket
这个题都想不出来,感觉
- CF938D Buy a Ticket dijkstra
考试T1,建一个反图跑一个最短路就好了~ code: #include <bits/stdc++.h> #define ll long long #define N 200002 #def ...
- cf自训4.10
cf933A dp题 一开始看错是连续子序列了,然后样例刚好能过.. 然后正解没想出来,网上看了题解:感觉正解是枚举2开始的位置,然后再枚举翻转的区间,pos左右两侧分别求出贡献最大的那个区间,左右两 ...
- NOIP前刷题记录
因为本蒻实在太蒻了...对于即将到来的NOIP2018ssfd,所以下决心要把自己近期做过的题目(衡量标准为洛谷蓝题难度或以上)整理一下,归归类,简单地写一下思路,就当作自己复习了吧qwq 本随笔持续 ...
- NOIP刷题
搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...
随机推荐
- qt creator源码全方面分析(0)
本人主攻C++和Qt. 上两天刚研究完Qt install framework(IFW)应用程序安装框架. google没发现有正儿八经的官方文档的翻译,我就进行了翻译哈!! 系列文章具体见:http ...
- CTF--HTTP服务--路径遍历(拿到www-data用户权限)
开门见山 1. 扫描靶机ip,发现PCS 172.18.4.20 2. 用nmap扫描靶机开放服务及版本 3. 再扫描靶机的全部信息 4. 用nikto工具探测http服务敏感信息 5. 用dirb工 ...
- day05【数组】
day05[数组] 1.数组 概念:是一种容器,可以同时存放多个数据. 特点: 数组是一种引用数据类型 数组当中的多个数据,类型必须统一 数组的长度在程序的运行期间不可改变 初始化:在内存当中创建一个 ...
- Leetcode 题目整理-1
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- acwing 243. 一个简单的整数问题2 树状数组 线段树
地址 https://www.acwing.com/problem/content/description/244/ 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l ...
- react中 如何使用图片
//render 第一种方法:先const一个对象,把需要应用图片的dom上的style写入对象中, 然后在return()中使用style关键字赋值为预先定义的那个style对象 const bgG ...
- requestAnimationFrame 与 seeTimeout 的区别
requestAnimationFrame 随着浏览器的刷新而执行. let a = () =>{ doSomething() ; window.reques ...
- JVM性能优化系列-(3) 虚拟机执行子系统
3. 虚拟机执行子系统 3.1 Java跨平台的基础 Java刚诞生的宣传口号:一次编写,到处运行(Write Once, Run Anywhere),其中字节码是构成平台无关的基石,也是语言无关性的 ...
- TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错
报错Can't instantiate abstract class Ultraman with abstract methods sttack 通过非常仔细的排查,发现错误如下: 1.单词拼写错误是 ...
- 使用小书匠及markdown here编辑博客和微信公众号
1. 使用小书匠连接Evernote并发布笔记到博客园 1.1 小书匠初探 我平时的信息收集的主要方法是采用Pocket+Evernote. 简单来说: 如果访问到非常有用,而且是必须要立刻记录的内容 ...