题意

求一个生成树,使得任意点到源点的最短路等于原图中的最短路。再让这个生成树边权和最小。

http://codeforces.com/contest/545/problem/E

思路

先Dijkstra一下,再对每个点连的边判断是不是最短路上的边,如果是那再贪心取最小的边即可。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e18;
const int N=6e5+5;
struct qnode
{
ll v;
ll c;
qnode(ll _v=0,ll _c=0):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct edge
{
ll v,cost;
int next;
};
edge eg[N];
int head[N];
ll dist[N],tot=1;
bool vis[N];
void Dijkstra(int n,int sta)
{
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++) dist[i]=inf;
priority_queue<qnode> pq;
dist[sta]=0;
pq.push(qnode(sta,0));
qnode tmp;
while(!pq.empty())
{
tmp=pq.top();
pq.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=head[u];~i;i=eg[i].next)
{
int v=eg[i].v;
int cost=eg[i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
pq.push(qnode(v,dist[v]));
}
}
}
}
void init()
{
memset(head,-1,sizeof(head));
tot=1;
}
void addedge(int u,int v,ll w)
{
eg[tot].v=v;
eg[tot].cost=w;
eg[tot].next=head[u];
head[u]=tot++;
}
int main()
{
int n,m;
init();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
int rt;
scanf("%d",&rt);
Dijkstra(n,rt);
vector<int> ans;
ll sum=0;
for(int i=1;i<=n;i++)
{
if(i!=rt)
{
ll mn=inf,mi;
for(int j=head[i];~j;j=eg[j].next)
{
int v=eg[j].v;
if(dist[i]==dist[v]+eg[j].cost)
{
if(mn>eg[j].cost)
{
mn=eg[j].cost;
mi=(j+1)/2;
}
}
}
ans.push_back(mi);
sum+=mn;
}
}
printf("%lld\n",sum);
for(int i : ans)
{
printf("%d ",i);
}
puts("");
return 0;
}

Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)的更多相关文章

  1. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  2. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  3. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  4. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

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

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

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

  8. Codeforces Round #303 (Div. 2)

    A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...

  9. Codeforces Round #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

随机推荐

  1. go读取配置模块viper

    这个可以常常和cobra配合. 来个demo package main import ( "fmt" "github.com/spf13/viper" ) fu ...

  2. 2019年最新50道java基础部分面试题

    [软帝学院]1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语法,集合的语法,io 的语法,虚拟机方面的语法. 1.一个".j ...

  3. vue 使用watch监听实现类似百度搜索功能

    watch监听方法,watch可以监听多个变量,具体使用方法看代码: HTML: <!doctype html> <html lang="en"> < ...

  4. Unity 2018 Cookbook (Matt Smith 著)

    1. Displaying Data with Core UI Elements (已看) 2. Responding to User Events for Interactive UIs (已看) ...

  5. Paper | Xception: Deep Learning with Depthwise Separable Convolutions

    目录 故事 Inception结构和思想 更进一步,以及现有的深度可分离卷积 Xception结构 实验 这篇论文写得很好.只要你知道卷积操作或公式,哪怕没看过Inception,也能看懂. 核心贡献 ...

  6. sed命令常用用法

    1.字符串替换 sed -i "s/xxx/yyy/g" /home/test.log // 将home目录下的test.txt文件中的所有xxx字符串替换成yyy字符串 sed ...

  7. LeetCode 225:用队列实现栈 Implement Stack using Queues

    题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...

  8. IIS锁定是默认设置的 (overrideModeDefault="Deny")问题解决

    发布网站时提示错误 锁定是默认设置的 (overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 ...

  9. Windows cmd 和 PowerShell 中文乱码问题解决

    临时方案: 在命令行下输入:chcp 65001 长期方案: 要修改注册表,自己网上搜吧

  10. 分布式Redis深度历险-Cluster

    本文为分布式Redis深度历险系列的第三篇,主要内容为Redis的Cluster,也就是Redis集群功能. Redis集群是Redis官方提供的分布式方案,整个集群通过将所有数据分成16384个槽来 ...