[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 ...
随机推荐
- [Go]结构体及其方法
结构体类型可以包含若干字段,每个字段通常都需要有确切的名字和类型.也可以不包含任何字段,这样并不是没有意义的,因为还可以为这些类型关联上一些方法,这里可以把方法看作事函数的特殊版本. 函数事独立的程序 ...
- B题 Sort the Array
题目大意:判断能否通过一次倒置,使序列变为一个递增序列 如果可以,输出倒置那一段的起始点和终点的位置: 题目链接:http://codeforces.com/problemset/problem/45 ...
- 【字符串+BFS】Problem 7. James Bond
https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个 ...
- 【BZOJ1031】字符加密Cipher(后缀数组)
题意:将一个长度为2n(复制粘贴后)的字符串的所有长度为n的后缀从小到大排序,并依次输出它们的最后一个字母. n<=100000 思路:裸SA,模板真难背 P党不得不写成C++风格 ..]of ...
- VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]
传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- msp430入门编程30
msp430中C语言的文件管理
- HDU 5521 Meeting【最短路】
今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...
- POJ 2488 A Knight's Journey【DFS】
补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- Intersection--poj1410(判断线段与矩形的关系)
http://poj.org/problem?id=1410 题目大意:给你一个线段和矩形的对角两点 如果相交就输出'T' 不想交就是'F' 注意: 1,给的矩形有可能不是左上 右下 所以要先判 ...
- 信号量学习 & 共享内存同步
刚刚这篇文章学习了共享内存:http://www.cnblogs.com/charlesblc/p/6142139.html 里面也提到了共享内存,自己不进行同步,需要其他手段比如信号量来进行.那么现 ...