CF 545E Paths and Trees
题目大意:给出n个点,m条无向边,每条边有长度。求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小。输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m).
思路 :单源最短路径 + 贪心 .用Dijkstra 或spfa 算法 求每个点到源点的最短路径,并在记录当前用来更新每个点最短距离的那条边(即pre[i]记录点i所对应的变)。
若当前这条边能使得某个点到源点的距离变小则更新该点到源点的距离,并记录更新该点的边; 若当前这条边更新某节点后,使得该点到源点的距离不变(即:d[e[t].to] = d[e[t].from] + e[t].length ) 则比较这条边与当前该点所记录的边的长度大小,若这条边比改点所记录的边长度要小(即e[pre[e[t].to]].length > e[t].length),则更新改点所记录的边(pre[e[t].to] = t)。
代码 :
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<queue>
#define INF 0x7fffffffffffffff
#define pii pair<long long,long long>
using namespace std;
long long s,n,m,pre[300002],tag[600002],u[600002],v[600002],next[600002],first[300002];
long long sum,d[300002],w[600002];
priority_queue<pii,vector<pii>,greater<pii> > Q;
bool vis[300002]; void input(){
long long i,j,a,b,c;
for(i=1;i<=n;i++)
first[i] = -1;
for(i=1;i<=m;i++){
scanf("%I64d %I64d %I64d",&u[i],&v[i],&w[i]);
u[i+m] = v[i] ;
v[i+m] = u[i] ;
w[i+m] = w[i] ;
next[i] = first[u[i]];
first[u[i]] = i ;
next[i+m] = first[v[i]];
first[v[i]] = i+m ;
}
scanf("%I64d",&s);
} void Dijkstra(){
long long i,j;
memset(vis,0,sizeof(vis));
memset(tag,0,sizeof(tag));
for(i=1;i<=n;i++)
pre[i] = -1 ;
for(i=1;i<=n;i++)
d[i] = INF;
d[s] = 0;
Q.push(make_pair(d[s],s));
while(!Q.empty()){
pii t = Q.top();
Q.pop();
long long x = t.second;
if(vis[x])
continue;
vis[x] = true;
for(long long e=first[x]; e!=-1; e=next[e]){
if(d[v[e]] > d[x] + w[e] && d[x] < INF){
pre[v[e]] = e;
d[v[e]] = d[x] + w[e] ;
Q.push(make_pair(d[v[e]],v[e]));}elseif(d[v[e]]== d[x]+ w[e]&& d[x]< INF && w[pre[v[e]]]> w[e]){
pre[v[e]]= e;
Q.push(make_pair(d[v[e]],v[e]));}}}}int main(){longlong i,j;while(scanf("%I64d%I64d",&n,&m)==2){
input();
sum =0;Dijkstra();for(i=1;i<=n;i++){
sum += w[pre[i]];
tag[pre[i]]=1;} printf("%I64d\n",sum);longlong count =0;for(i=1;i<=2*m;i++)if(tag[i]){if(i > m){if(count !=0)
printf(" %I64d",i-m);else
printf("%I64d",i-m);}elseif(i<=m){if(count !=0)
printf(" %I64d",i);else
printf("%I64d",i);}
count ++;}
printf("\n");}return0;}
CF 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 ...
- 545E. Paths and Trees
题目链接 题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号. 思路:先求一遍 ...
- Codeforces 545E. Paths and Trees[最短路+贪心]
[题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...
- [Codeforces 545E] Paths and Trees
[题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边 答 ...
- 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 ...
随机推荐
- hdu4521-小明系列问题——小明序列(线段树区间求最值)
题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为 线段树解法. #include ...
- 如何保存JMeter的性能测试数据到ElasticSearch上,并且使用Kibana进行可视化分析(1)
前言 Jmeter是一款性能测试,压力测试的开源工具,被大量的测试人员拿来测试产品的性能,负载等等. Jmeter除了强大的预置的各种插件,各种可视化图表工具以外,也有些固有的缺陷,例如: 我们往往只 ...
- Bubble Sort 冒泡排序
//Bubble Sort ( O(n²)) public class TestBubbleSort { public int[] bubbleSortArray(int[] arr){ ; i &l ...
- WebService- 使用 CXF 开发 SOAP 服务
选框架犹如选媳妇,选来选去,最后我还是选了“丑媳妇(CXF)”,为什么是它?因为 CXF 是 Apache 旗下的一款非常优秀的 WS 开源框架,具备轻量级的特性,而且能无缝整合到 Spring 中. ...
- 如何将XML转换成XSD(XML Schema)文件
将xml装换为xsd,先决条件是已经安装了Visual Stutio 1) 输入cmd在运行窗口 2) 将xsd的路径加入到path变量 set path=%path%;C:\Program File ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
- 搭建完全分布式的hadoop[转]
hadoop 创建用户及hdfs权限,hdfs操作等常用shell命令 sudo addgroup hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry# ...
- [Cycle.js] Main function and effects functions
We need to give structure to our application with logic and effects. This lessons shows how we can o ...
- [Regular Expressions] Find Repeated Patterns
Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...
- 字符串匹配之horspool算法(简化的BM算法)
前面介绍在BF,KMP这些算法的时候老是提到BM这个东西,究竟这什么东西,有啥高深的,这些问题我们如今不去考虑.不知道,认真读前几篇文章的读者有没有发现前面的算法都是从模式串的前面開始匹配的,那我们就 ...