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 ...
随机推荐
- C++中的传值与传址
在指针的传递中,也涉及到传值与传址的问题.下面通过一个函数进行说明. 代码如下: bool openBinary(uchar* buffer) { ; buffer = (uchar*)malloc( ...
- office 小技巧
1. 数据匹配 VLOOKUP 例一:如图,将左表(sheet1)的e列关联到右表(sheet2),条件为: sheet2.字段名 = sheet1.D 公式:=VLOOKUP(sheet2!D2:D ...
- 【声明式事务】Spring声明式事务实现(三)
以MyBatis为例. 一.基于注解的声明式事务配置 1. 添加tx名字空间 xmlns:tx="http://www.springframework.org/schema/tx" ...
- html头部标签汇总
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...
- Python 对象比较(is & ==)
Python 对象有 3 要素 id type value id 对象在内存中的地址 可以通过 id() 获取 比较 只有同一个对象 id 才会相同 id 通过 is 比较 示例: a = list( ...
- WinSxS目录下文件的清除
1)McAfee Scanner service 持续高cpu 2)上网查到了,需要看 %deflogdir%目录下的OnDemandScan_Activity.log 3) 打开这个文件,发觉一直在 ...
- 9.28 csp-s模拟测试54 x+y+z
T1 x 求出每个数的质因数,并查集维护因子相同的数,最后看一共有多少个联通块,$ans=2^{cnt}-2$ 但是直接分解会$T$,埃筛是个很好的选择,或者利用每个数最多只会有1个大于$\sqrt{ ...
- IPv6 邻居状态迁移
- Kafka随笔
1.选举Leader Leader 是 Partition 级别的,当一个 Broker 挂掉后,所有 Leader 在该 Broker 上的 Partition 都会被重新选举,选出一个新 Lea ...
- 常用的排列、组合、阶乘函数 MATLAB
1.求n的阶乘,方法如下:a.factorial(n)b.gamma(n+1)c.v='n!'; vpa(v) 2.求组合(数),方法如下:a.combntns(x,m) 列举出从n个元素中取出 ...