[题目链接]

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的更多相关文章

  1. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  2. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  3. 545E. Paths and Trees

    题目链接 题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号. 思路:先求一遍 ...

  4. CF 545E Paths and Trees

    题目大意:给出n个点,m条无向边,每条边有长度.求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小.输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m). 思路 : ...

  5. codeforces 545E E. Paths and Trees(单源最短路+总权重最小)

    E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Paths and Trees

    Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...

随机推荐

  1. 数据库连接 Mysqli

    //数据库连接 Mysqli $db= new Mysqli("localhost","root","root","asd_808 ...

  2. HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) http://acm. ...

  3. SPOJ FAVDICE 数学期望

    题目大意: 一个有n面的色子抛掷多少次能使所有面都能被抛到过,求期望值 总面数为n,当已经抛到过 i 个不同面时,我们抛出下一个不同面的概率为 (n-i)/n,那么抛的次数为 n/(n-i) 将所有抛 ...

  4. POJ 2420 A Star not a Tree?【爬山法】

    题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...

  5. Java描述符(修饰符)的类型

    以下内容引用自http://wiki.jikexueyuan.com/project/java/modifier-types.html: 描述符(修饰符)是添加到那些定义中来改变他们的意思的关键词.J ...

  6. 一次mysql 优化 (Using temporary ; Using filesort)

    遇到一个SQL执行很慢 SQL 如下: SELECT ... FROM tableA WHERE time >= 1492044535 and time <= 1492046335 GRO ...

  7. [TypeScript] Query Properties with keyof and Lookup Types in TypeScript

    The keyof operator produces a union type of all known, public property names of a given type. You ca ...

  8. Codeforces div.2 B. The Child and Set

    题目例如以下: B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input ...

  9. 微信小程序之 页面跳转 及 调用本地json的假数据调试

    一.微信小程序 跳转页面 小程序页面有2种跳转,可以在wxml页面或者js中: (1)在wxml页面中: <navigator url="../index/index"> ...

  10. ascii与unicode,utf-8小结

    ascii是以一个字节存储英文和特殊字符,不支持中文的处理.unicode占用的是两个字节,可以存储中文.utf-8占用三个字节,可以根据存储的内容进行中英文的转换. Python的解释器是不支持中文 ...