Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)
3 seconds
256 megabytes
standard input
standard output
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a connected weighted undirected graph G = (V, E) (here Vis the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graphG1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1are the same.
You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.
The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.
Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.
In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them.
3 3
1 2 1
2 3 1
1 3 2
3
2
1 2
4 4
1 2 1
2 3 1
3 4 1
4 1 2
4
4
2 3 4
In the first sample there are two possible shortest path trees:
- with edges 1 – 3 and 2 – 3 (the total weight is 3);
- with edges 1 – 2 and 2 – 3 (the total weight is 2);
And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.
题目:一个带权无向图,求一个新图G’=(V,E’),使得源点s到新图各个点的最短距离等于在原图中的最短距离,输出边权值最小的新图;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=300000; int used[maxn+10],cost[maxn+10],flag[maxn+10],vis[maxn+10];
ll dis[maxn+10]; struct Edge{
int to,w,id;
};
vector<Edge> G[maxn+10]; struct node{
int to;
ll dis;
bool operator <(const node a) const{
return this->dis>a.dis;
}
};
int main()
{
int n,m,u,v,w,s;
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
G[u].push_back((Edge){v,w,i});
G[v].push_back((Edge){u,w,i});
} scanf("%d",&s);
for(int i=1;i<=n;i++)
{
dis[i]=1e16;
cost[i]=1e9+10;
flag[i]=0;
used[i]=0;
}
priority_queue<node> q;
q.push((node){s,0});
dis[s]=0;
while(q.size())
{
node cur=q.top();q.pop();
if(dis[cur.to]<cur.dis) continue;
int u=cur.to;
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
Edge e=G[u][i];
if(used[e.to]) continue;
if(dis[e.to]>dis[u]+e.w)
{
dis[e.to]=dis[u]+e.w;
flag[e.to]=e.id;
cost[e.to]=e.w;
q.push((node){e.to,dis[e.to]});//q要放在更新函数内,否则会爆优先队列
}
else if(dis[e.to]==dis[u]+e.w&&cost[e.to]>e.w)
{
cost[e.to]=e.w;
flag[e.to]=e.id;
q.push((node){e.to,dis[e.to]});
}
}
} ll ans=0;
for(int i=1;i<=n;i++)
if(i!=s) ans+=cost[i];
printf("%lld\n",ans);
for(int i=1;i<=n;i++)
if(i!=s) printf("%d ",flag[i]);
printf("\n");
}
return 0;
}
分析:神奇的一道题目,
1.需要使用堆优化的Dijkstra求一遍最短路;
2.贪心:在求最短路的过程中,假如起点到其余各点只有一条最短路的话,那么显然就是这些最短路
组成的图,但是假如到达同一个点有多条最短路的话,那么就要进行贪心,比如样例中的2和3号节点都可以
最短路到达1,但是因为2与1直接相连的那条边权值要小,所以就选走2这条路的。
3.其实求最后的起点到每个节点的最短路,就是一棵树
Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)的更多相关文章
- 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) 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)
A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...
- Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...
- Codeforces Round #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
随机推荐
- Visual Subst - 简单将任意文件夹挂载模拟成驱动器盘符硬盘分区的小工具
随着电脑的使用,硬盘里的资料一天比一天多,也越来越杂乱.一些朋友为了方便文件管理,会考虑重新分区,让C.D.E等盘符分别担任不同的角色.不过,不分区的话也有一些小工具可以帮你实现. Visual Su ...
- JavaScript Array Reduce用于数组求和
需求一 假设有一个数组,需要对其中的元素进行求和. const numbers = [1, -1, 2, 3]; 传统写法,使用for循环求和 const numbers = [1, -1, 2, 3 ...
- 最新的省市编码和sql
下面的项目是整理的最新的省市编码sql文件,可以看看. github
- CSP-S全国模拟赛第三场 【nan死了】
mmt 居然第一步膜化乘除 都没看出来,没救了... 大概是贡献前缀和优化的做法 巨兔式讲解:大家都学会了么? 咱发现有大量的 (i/j , i%j ) 同时 对很多 c 产生了贡献,咱可以去优化这一 ...
- 运维LVS-NAT模式理解
一.LVS-NAT模式的工作原理这个是通过网络地址转换的方法来实现调度的.首先调度器(LB)接收到客户的请求数据包时(请求的目的IP为VIP),根据调度算法决定将请求发送给哪个 后端的真实服务器(RS ...
- springmvc中的全注解模式
1.贴在类上: @Controller表明其是一个控制器 2.贴在方法上: @requestMapping("/xxx"): 标明请求要访问的方法的资源路径,,需以/打头.其中省略 ...
- win7系统下MongoDB 4.0.1的安装
环境: win7 - 64位系统 MongoDB下载地址: https://www.mongodb.com/download-center#community 版本: 4.0.1 安装步骤: 选择cu ...
- 关于 i++ 和 ++ i
先看一下代码,猜想一下输出值 @Testpublic void test() { int i =1; int a,b=0; i++; a=(i++); System.out.println(a); S ...
- bootloader架构设计
G-boot架构设计 第一阶段程序设计 1.0.核心初始化: 1.设置中断向量表 2.设置处理器为svc模式 3.关闭看门狗 4.关闭所有中断 5.关闭mmu和cache 6.外设基地址初始化 ...
- linux系统监控sar命令
linux系统监控sar命令详解 sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包 ...