[Codeforces 545E] Paths and Trees
[题目链接]
https://codeforces.com/contest/545/problem/E
[算法]
首先求 u 到所有结点的最短路
记录每个节点最短路径上的最后一条边
答案即为以u为根的一棵最短路径生成树
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + ;
const long long INF = 1e60; struct edge
{
int to , w , nxt;
} e[MAXN << ]; int n , m , s , tot;
int head[MAXN],u[MAXN],v[MAXN],w[MAXN],last[MAXN];
long long dist[MAXN];
bool visited[MAXN],vis[MAXN << ]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v,int w)
{
tot++;
e[tot] = (edge){v,w,head[u]};
head[u] = tot;
}
inline void dijkstra(int s)
{
priority_queue< pair<long long,int> , vector< pair<long long,int> > ,greater< pair<long long,int> > > q;
for (int i = ; i <= n; i++)
{
dist[i] = INF;
visited[i] = false;
}
dist[s] = ;
q.push(make_pair(,s));
while (!q.empty())
{
int cur = q.top().second;
q.pop();
if (visited[cur]) continue;
visited[cur] = true;
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (!visited[v] && dist[cur] + w <= dist[v])
{
if (dist[cur] + w < dist[v]) last[v] = i;
else if (w < e[last[v]].w) last[v] = i;
dist[v] = dist[cur] + w;
q.push(make_pair(dist[v],v));
}
}
}
} int main()
{ read(n); read(m);
for (int i = ; i <= m; i++)
{
read(u[i]); read(v[i]); read(w[i]);
addedge(u[i],v[i],w[i]);
addedge(v[i],u[i],w[i]);
}
read(s);
dijkstra(s);
for (int i = ; i <= n; i++) vis[last[i]] = true;
vector< int > ans;
long long res = ;
for (int i = ; i <= tot; i += )
{
if (vis[i] || vis[i - ])
{
ans.push_back(i >> );
res += e[i].w;
}
}
printf("%I64d\n",res);
for (unsigned i = ; i < ans.size(); i++)
if (i == ) printf("%d",ans[i]);
else printf(" %d",ans[i]);
printf("\n"); return ; }
[Codeforces 545E] Paths and Trees的更多相关文章
- Codeforces 545E. Paths and Trees 最短路
E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...
- Codeforces 545E. Paths and Trees[最短路+贪心]
[题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...
- 545E. Paths and Trees
题目链接 题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号. 思路:先求一遍 ...
- CF 545E Paths and Trees
题目大意:给出n个点,m条无向边,每条边有长度.求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小.输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m). 思路 : ...
- codeforces 545E E. Paths and Trees(单源最短路+总权重最小)
E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...
- Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路
E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)
E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Paths and Trees
Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...
随机推荐
- Flask设计带认证token的RESTful API接口[翻译]
上一篇文章, 使用python的Flask实现一个RESTful API服务器端 简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...
- 65.什么是IOC?【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 这个小节吧,是无意当中看了一篇文章,觉得介绍的特别好,引用到我的博客中,让大家也乐下.那么他是怎么解说IOC的呢?看如下: 套用好莱坞的一句名言就 ...
- vagrant的学习 之 Laravel
vagrant的学习 之 Laravel 本文根据慕课网的视频教程练习,感谢慕课网! 慕课视频学习地址:https://www.imooc.com/video/14218. 慕课的参考文档地址:htt ...
- linux 常见名词及命令(二)
pwd 用于显示当前的工作目录. cd 用于切换工作路径 cd - 切换到上一次的目录 cd ~ 切换到家目录 cd ~username 切换到其他用户的家目录 cd .. 切换到上级目录 ls 用于 ...
- Codeforces 653D Delivery Bears【二分+网络流】
题目链接: http://codeforces.com/problemset/problem/653/D 题意: x个熊拿着相同重量的物品,从1号结点沿着路走到N号结点,结点之间有边相连,保证可以从1 ...
- 洛谷——P2820 局域网
P2820 局域网 题目背景 某个局域网内有n(n<=100)台计算机,由于搭建局域网时工作人员的疏忽,现在局域网内的连接形成了回路,我们知道如果局域网形成回路那么数据将不停的在回路内传输,造成 ...
- Spring Boot+Profile实现不同环境读取不同配置
文件结构如下: 但是官方推荐放在config文件夹下. 作用: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中.prod环境下的配 ...
- Entity framework自定义字段实现思路
ublic class MyModel { public int MyModelID { get; set; } public string FixedProperty1 { get; set; } ...
- Meteor ToDo App实例
在本章中,我们将创建一个简单的待办事项应用程序. 第1步 - 创建应用程序 打开命令提示符,运行以下命令 - C:\Users\Administrator\Desktop>meteor crea ...
- 海量数据处理面试题学习zz
来吧骚年,看看海量数据处理方面的面试题吧. 原文:(Link, 其实引自这里 Link, 而这个又是 Link 的总结) 另外还有一个系列,挺好的:http://blog.csdn.net/v_jul ...