Codeforces Round #303 (Div. 2) E
五道水题,但要手快才好。。。我手慢了,E题目都没看完TAT....
想了一发,很水,就是一遍Dijk即可,使用优先队列,同时记录由哪条边转移而来
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define LL long long
using namespace std; const int MAXN=; struct Edge{
int u,v,w,i,next;
}edge[MAXN*];
int weight[MAXN],head[MAXN],tot;
int n,m; int chose[MAXN];
LL dis[MAXN]; bool vis[MAXN]; struct Node{
int u; LL d;
Node(){}
Node(int uu,LL dd){u=uu,d=dd;}
bool operator<(const Node &a)const{
return d>a.d;
}
}; vector <int>ans;
priority_queue<Node>pq;
LL cur; void addedge(int u,int v,int w,int i){
edge[tot].u=u; edge[tot].v=v;
edge[tot].w=w; edge[tot].i=i;
edge[tot].next=head[u];
head[u]=tot++;
} void Dijk(int r){
cur=;
for(int i=;i<=n;i++) dis[i]=1ll<<;
dis[r]=;
memset(chose,,sizeof(chose));
memset(vis,false,sizeof(vis));
ans.clear();
pq.push(Node(r,));
while(!pq.empty()){
Node tmp=pq.top(); pq.pop();
if(vis[tmp.u]) continue;
if(tmp.u!=r){
cur+=weight[chose[tmp.u]];
ans.push_back(chose[tmp.u]);
}
LL d=tmp.d;
vis[tmp.u]=true;
for(int e=head[tmp.u];e!=-;e=edge[e].next){
int v=edge[e].v;
if(!vis[v]){
if(d+edge[e].w<dis[v]){
dis[v]=d+edge[e].w;
chose[v]=edge[e].i;
pq.push(Node(v,dis[v]));
}
else if(d+edge[e].w==dis[v]){
if(edge[e].w<weight[chose[v]]){
chose[v]=edge[e].i;
}
}
}
}
}
sort(ans.begin(),ans.end());
cout<<cur<<endl;
int len=ans.size();
if(len>){
printf("%d",ans[]);
for(int i=;i<len;i++)
printf(" %d",ans[i]);
printf("\n");
}
} int main(){
int u,v,w;
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-,sizeof(int)*(n+));
tot=;
weight[]=1e9+;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
weight[i]=w;
}
scanf("%d",&u);
Dijk(u);
}
return ;
}
Codeforces Round #303 (Div. 2) E的更多相关文章
- 水题 Codeforces Round #303 (Div. 2) D. Queue
题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 水题 Codeforces Round #303 (Div. 2) A. Toy Cars
题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...
- Codeforces Round #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- Codeforces Round #303 (Div. 2) B. Equidistant String 水题
B. Equidistant String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
- Codeforces Round #303 (Div. 2) A. Toy Cars 水题
A. Toy Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...
- 「日常训练」Queue(Codeforces Round 303 Div.2 D)
简单到让人不敢相信是D题,但是还是疏忽了一点. 题意与分析 (Codeforces 545D) 题意:n人排队,当一个人排队的时间超过他需要服务的时间就会厌烦,现在要求一个最优排列使得厌烦的人最少. ...
- 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)
这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...
随机推荐
- Unity项目 - 捡苹果 Apple Picker
项目展示 Github项目地址:Apple Picker 涉及知识 正投视图 3D场景内树与苹果的图层 记录最高分到本地 准备工作 模型制作: 基本模型创建 树叶:sphere 拉伸为椭圆形,绿色材质 ...
- 判断IOS静态库(.a文件)是否支持模拟器和真机运行
判断IOS静态库(.a文件)是否支持模拟器和真机运行 在mac终端下,进入到.a文件目录下,然后输入: lipo -info libMyAlertView.a Architectures in the ...
- ZOJ3714JavaBeans
#!/usr/bin/env python # encoding: utf-8 t = int(raw_input()) for i in range(t): n,k = [int(x) for x ...
- NS2学习笔记(一)
NS2有两种运行方式: 1.“脚本方式”,输入命令: ns tclscripl.tcl,其中 tclscripl.tcl 是一个Tcl脚本的文件名: 2“命令行方式”,输入命令:ns,进入NS2的命令 ...
- POJ 3855 计算几何·多边形重心
思路: 多边形面积->任选一个点,把多边形拆成三角,叉积一下 三角形重心->(x1+x2+x3)/3,(y1+y2+y3)/3 多边形重心公式题目中有,套一下就好了 计算多边形重心方法: ...
- Jquery 多行拖拽图片排序 jq优化
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CSS——属性选择器
属性选择器:通过对标签中属性的选择,控制标签. <!DOCTYPE html> <html> <head> <style> div[class*=&qu ...
- nagios 安装pnp4nagios插件
Naigos install pnp4nagios 绘图插件 原文地址:http://www.cnblogs.com/caoguo/p/5022230.html [root@Cagios ~]# yu ...
- vue 与 angular 的区别
vue仅仅是mvvm中的view层,只是一个如jquery般的工具库,而不是框架,而angular而是mvvm框架. vue的双向邦定是基于ES5 中的 getter/setter来实现的,而angu ...
- PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unkno…
今天安装安装一个叫得推校园O2O系统的使劲都说连接不上服务器. 下面是安装说明,我直接进行3步骤,导入数据库配置文件,结果就显示题目报错的内容 安装说明: 直接输入程序目录即可 http://loca ...