题目链接

题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号。

思路:先求一遍最短路,从所有到i点的满足最短路的边中选一条权最小的边。

Java程序

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner; public class E545 {
private static class Edge {
int v;
long w;
int index; Edge(int v, long w, int index) {
this.v = v;
this.w = w;
this.index = index;
}
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintStream out = System.out; int n = in.nextInt(), m = in.nextInt();
List<Edge>[] graph = new List[n]; for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<E545.Edge>();
} for (int i = 1; i <= m; i++) {
int v1 = in.nextInt() - 1;
int v2 = in.nextInt() - 1;
long w = in.nextLong(); graph[v1].add(new Edge(v2, w, i));
graph[v2].add(new Edge(v1, w, i));
}
int u = in.nextInt() - 1; Edge[] lastEdge = new Edge[n];
final long[] min = new long[n];
for (int i = 0; i < n; i++) {
min[i] = -1;
} min[u] = 0;
Queue<Integer> q = new LinkedList<Integer>(); q.add(u); while (!q.isEmpty()) {
int v = q.poll(); for (Edge edge : graph[v]) {
int v1 = edge.v;
long min1 = min[v] + edge.w; if ((min[v1] == -1) || (min1 < min[v1])
|| (min1 == min[v1] && edge.w < lastEdge[v1].w)) { min[v1] = min1;
lastEdge[v1] = edge;
q.add(v1);
}
}
} long res = 0;
boolean[] f = new boolean[m]; for (int i = 0; i < n; i++) {
if (lastEdge[i] != null) {
res += lastEdge[i].w;
f[lastEdge[i].index - 1] = true;
}
} out.println(res); StringBuilder s = new StringBuilder();
boolean first = true;
for (int i = 0; i < m; i++) {
if (f[i]) {
if (!first) {
s.append(" ");
}
s.append(i + 1);
first = false;
}
}
out.println(s.toString());
in.close();
out.close(); } }

 

Python代码

import heapq as hq

class edge(object):
def __init__(self, to, w, nr):
self.to = to
self.w = w
self.nr = nr n, m = map(int, raw_input().split())
adj = [[] for _ in range(n + 1)]
for i in range(1, m+1):
u, v, c = map(int, raw_input().split())
adj[u].append((v, c, i))
adj[v].append((u, c, i))
root = int(raw_input())
vis = [False] * (n+1)
q = [(0, 0, root, 0)]
ans = []
tot = 0
while q:
d, c, n, e = hq.heappop(q)
if vis[n]:
continue
ans.append(e)
tot += c
vis[n] = True
for v, c, i in adj[n]:
if not vis[v]:
hq.heappush(q, (d+c, c, v, i))
ans = map(str, ans)
print tot
print " ".join(ans[1:])

 

上面的代码都是在codeforces上面抄过来的,自己写不出来。。。。

545E. Paths and Trees的更多相关文章

  1. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  2. CF 545E Paths and Trees

    题目大意:给出n个点,m条无向边,每条边有长度.求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小.输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m). 思路 : ...

  3. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  4. [Codeforces 545E] Paths and Trees

    [题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边         答 ...

  5. codeforces 545E E. Paths and Trees(单源最短路+总权重最小)

    E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

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

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

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

  9. Codeforces Paths and Trees

    Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...

随机推荐

  1. [转]理解与使用Javascript中的回调函数

    在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中被创建,能从函数中返回. 因 ...

  2. [转]安装openoffice,并且配置为windows服务

    [转]安装openoffice,并且配置为windows服务 http://blog.csdn.net/zzzz3621/article/details/18400277 下载windows reso ...

  3. Python实现NN(神经网络)

    Python实现NN(神经网络) 参考自Github开源代码:https://github.com/dennybritz/nn-from-scratch 运行环境 Pyhton3 numpy(科学计算 ...

  4. Function-两个日期大小比较

    function checkDate(from,to){ if (from == "" || to == "") return 2; var rValue = ...

  5. P1119: [POI2009]SLO

    这题预处理稍微动动脑,其实还是个裸的置换群=-=,没什么压力. ; var n,i,j,minx,tem,now,tmin,len:longint; cursum,sum:int64; pos,num ...

  6. 解决ubuntu字体发虚,网页字体发虚

    好吧,哥也不知所以然,只是突然间所有东西的字体都发虚了~~后来发现是应该是语言支持搞的鬼,卸载掉下面的东东就没事了 sudo apt-get remove fonts-arphic-ukai ttf- ...

  7. 安装配置tomcat

    1.安装nginx 下载nginx-1.4.3 解压: tar zxvf nginx-1.4.3.tar.gz 编译安装: ./configure --prefix=/app/act/nginx/ng ...

  8. 华为HG8240光猫-破解-联通-2016-telnet-http

    序 我与大家想法基本一致,拿到联通的光猫后,心想它应该是个路由器吧,如果让它自己拨号上网就好了,即省一台路由器,又省电了.抱着这个想法,在2013年里,我搜罗了不少文章,经过Q群,搜索,询问,阅读,理 ...

  9. 读书笔记:<世界是数字的>

    世界是数字的?第一次听到这个名词不由的感到惊讶,我有听过地球是海洋的,那世界不应该是人类共同拥有的吗或者也可以说世界是大自然的?为什么世界偏偏是数字的呢?我带着满心的疑问去看了那本<世界是数字的 ...

  10. 03.RedisJava客户端Jedis的使用

    1.Jedis基本使用 使用Jedis客户端使用Redis服务与在服务器上通过redis-cli使用命令基本一样,关于Redis命令请参考:http://www.redis.cn/commands.h ...