Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
题意
求一个生成树,使得任意点到源点的最短路等于原图中的最短路。再让这个生成树边权和最小。
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(最短路+贪心)的更多相关文章
- 水题 Codeforces Round #303 (Div. 2) D. Queue
题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 水题 Codeforces Round #303 (Div. 2) A. Toy Cars
题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...
- 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 Round #303 (Div. 2)
A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...
- Codeforces Round #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
随机推荐
- 01-Django 简介
一.MVC框架(模型-视图-控制器缩写,软件的构建模式) 一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需 ...
- 01 less的使用
使用less 安装两个包 1===>cnpm install less less-loader --save-dev less中的注释 以 //开头的注释 不会被编译到css文件中去 以 /** ...
- day64_10_8 vue的导入
一.简介 vue是一个渐进式的js框架.具体介绍查看官网: https://cn.vuejs.org 在vue框架中,有两个文件,当在开发阶段时使用完整版vue,因为有报错信息,而在上市阶段可以使用m ...
- Spring配置文件中的那些标签
1. context:annotation-config 它的作用是隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor,CommonAnnotatio ...
- verilog 常见单元描述
半加器: //行为级建模 module half_adder2(a, b, sum, c_out); input a, b; output sum, c_out; assign {c_out, sum ...
- github 入门教程之 github 访问速度太慢怎么办
github 是全世界最流行的开源项目托管平台,其代表的开源文化从根本上改变了软件开发的方式. 基本上所有的需求都能从 github 上或多或少找到现成的实现方案,再也不用重头开始造轮子而是自定义轮子 ...
- P2P中的NAT穿越(打洞)方案详解
一.P2P(点对点技术) 点对点技术(peer-to-peer,简称P2P)又称对等互联网络技术,是一种网络新技术,依赖网络中参与者的计算能力和带宽,而不是把依赖都聚集在较少的几台服务器上. 点对点技 ...
- js json字符串与json对象互相转换(最全)
1.json字符串转json对象 使用场景:通常在取json字符串里具体的值时,会用到. var jsonString = '{"name":"Marydon&quo ...
- 聊一下,前后分离后带来的跨域访问和cookie问题
在谈前后分离前,我们先看看什么是前后一体的.当我们用javaweb开发网站时,最终我们渲染的jsp或者springthymeleaf.我们的页面其实是WEB-INFO或者templates下.当用户请 ...
- Centos7安装percona-xtrabackup2.4和8.0版本
Percona XtraBackup是一个基于MySQL的服务器的开源热备份实用程序 ,它不会在备份期间锁定您的数据库.无论是24x7高负载服务器还是低事务量环境,Percona XtraBackup ...