Codeforces 545E. Paths and Trees[最短路+贪心]
[题目大意]
题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树。
题目要求一颗最短生成树,输出总边权和与选取边的编号。
[题意分析]
比如下面的数据:
5 5
1 2 2
2 3 2
3 4 16
1 5 18
4 5 2
1
这个图对于从 1 出发,有两种最短路。

这种最短路方案中 dis[2]=2,dis[3]=4,dis[4]=20,dis[5]=18。边权总和 Sum=44

但如果这样选边,1点到各点的距离依然为最短路,但Sum降为了24。
那么如何选择到最优的方案呢。 由于最短路方案构成的图一定是一棵树,所以我们可以将 除源点外 的所有顶点搞一个贪心。
改写dijkstra算法(c[]数组用来存取与该点连接的边的cost,s[]数组用来存取与该点连接的边的编号):
const ll inf=0x7fffffffffffff;
struct Edge{
int to,cost,id;
Edge(int T,int C,int D):to(T),cost(C),id(D){}
};
typedef pair<long long,int> P;
vector<Edge>G[];
ll d[],c[];
void dij(int u){
fill(d,d+n+,inf);
fill(c,c+n+,inf);
d[u]=;
priority_queue<P,vector<P>,greater<P>> q;
q.push(P(d[u],u));
while(!q.empty()){
P p=q.top();
q.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++){
Edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
s[e.to]=e.id;
c[e.to]=e.cost;
q.push(P(d[e.to],e.to));
}
else if(d[e.to]==d[v]+e.cost&&c[e.to]>e.cost){
s[e.to]=e.id;
c[e.to]=e.cost;
}
}
}
}
当有多条路通往一个顶点,且这两种路线cost相同时,我们可以将 通往该顶点的边 和 该顶点 绑定起来,我们希望每个顶点都能绑定合法的(不影响最短路的)且权值最小的边。
在dijkstra算法中我们每次挑选优先队列中权值最小的路。当碰上两条路cost相等的时候,我们选择与下一个点连接且边权最小的那一条边与下一个点绑定(Cost[nextV]=e.cost)。
* 由于最短路中从源点通往每个点的路可以组成一颗树 且 点只与从源点走向该点的边相绑定,所以一个点 一定可以 绑定一条边。
选取完毕后,我们只需要遍历 c[] 数组求出总花费,再遍历 s[] 数组输出每个点绑定的边的编号即可。
***Code:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int m,n,s[];
const ll inf=0x7fffffffffffff;
struct Edge{
int to,cost,id;
Edge(int T,int C,int D):to(T),cost(C),id(D){}
};
typedef pair<long long,int> P;
vector<Edge>G[];
ll d[],c[];
void dij(int u){
fill(d,d+n+,inf);
fill(c,c+n+,inf);
d[u]=;
priority_queue<P,vector<P>,greater<P>> q;
q.push(P(d[u],u));
while(!q.empty()){
P p=q.top();
q.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++){
Edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
s[e.to]=e.id;
c[e.to]=e.cost;
q.push(P(d[e.to],e.to));
}
else if(d[e.to]==d[v]+e.cost&&c[e.to]>e.cost){
s[e.to]=e.id;
c[e.to]=e.cost;
}
}
}
}
int main(){
int u,v,co;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&co);
G[u].push_back(Edge(v,co,i));
G[v].push_back(Edge(u,co,i));
}
scanf("%d",&u);
dij(u);
ll sum=;
for(int i=;i<=n;i++){
if(i!=u) sum+=c[i];
}
cout<<sum<<endl;
for(int i=;i<=n;i++){
if(i!=u) cout<<s[i]<<' ';
}
return ;
}
2017/7/13
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 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 545E] Paths and Trees
[题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边 答 ...
- 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 ...
- 545E. Paths and Trees
题目链接 题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号. 思路:先求一遍 ...
- Codeforces 1076D Edge Deletion 【最短路+贪心】
<题目链接> 题目大意: n个点,m条边的无向图,现在需要删除一些边,使得剩下的边数不能超过K条.1点为起点,如果1到 i 点的最短距离与删除边之前的最短距离相同,则称 i 为 " ...
- 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 Dijkstra堆优化+贪心(!!!)
E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- Android adb命令,linux中各种命令
常用的ADB命令 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器 ...
- iOS 通知、本地通知和推送通知有什么区别? APNS机制。
本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...
- Android线程池(转)
.前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52415337 使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用, ...
- uvm_factory——我们的工厂(三)
现在让我们回过头来想想factory 是用来干什么,它做了什么? fantory就是生产uvm_object 和 uvm_component.用factory 生产和用SV直接new有什么区别了? f ...
- Hadoop集群_VSFTP安装配置
原作者写的太好了,我这个菜鸟不自觉就转载了,原文链接:http://www.cnblogs.com/xia520pi/archive/2012/05/16/2503864.html 如果,您认为阅读这 ...
- Alpha-beta pruning
function alphabeta(node, depth, α, β, maximizingPlayer) or node is a terminal node return the heuris ...
- PostgressSQL-Installation
安装 sudo apt install -y postgresql 自动生成一个名为 postgres 的 Linux 系统用户 $ finger postgres Login: postgres N ...
- html备忘录
上传文件 <form action="/ajax/" method="post" enctype="multipart/form-data&qu ...
- WPF知识点全攻略09- 附加属性
附加属性也是一种特殊的依赖属性. Canvas中的Canvas.Left,Canvas.Top ,DockPanel中DockPanel.Dock等就是附加属性. 更加.NET类属性的写法经验.这个中 ...
- ratio_to_report分析函数求占比
drop table test; create table test ( name varchar(20), kemu varchar(20), score number ); insert int ...