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 ...
随机推荐
- poj 2407 Relatives(简单欧拉函数)
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...
- python3-day3(内置函数)
1.内置函数 1>print(bytearray('王',encoding='utf8')) 2>print(bytes('王',encoding='utf8')) 3>bool(' ...
- DenyHosts限制SSH登录尝试次数
DenyHosts官方网站为:http://denyhosts.sourceforge.net 1. 安装 # tar -zxvf DenyHosts-2.6.tar.gz # cd DenyHost ...
- Python安装MySQLdb并连接MySQL数据库
当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...
- spring 中StoredProcedure的使用方法
StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate运行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...
- 重写OnPaint事件对窗体重绘(显示gif动画) 实例2
/// <summary> /// 可显示Gif 的窗体 /// </summary> public class WinGif : Form { private Image _ ...
- AngularJs练习Demo6
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- 修饰器模式(day04)
修饰器设计模式 --最近我给女朋友买了一款可以更换外壳的手机.现在的外壳是红色的,假如我想用这款手机的时候,会更换成银灰色的外壳.但是我不能随意更换天线或者话筒,因为这些功能模块在手机生产的时候就已经 ...
- JDBC中PreparedStatement和Statement的区别
共同点: PreparedStatement和Statement都是用来执行SQL查询语句的API之一. 不同点: 在PreparedStatement中,当我们经常需要反复执行一条结构相似的sql语 ...
- PAT 65. A+B and C (64bit) (20)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1065 思路分析: 1)对a+b造成的long long 类型的数据溢出进行特殊处理: a> ...