题目链接:http://codeforces.com/contest/1076/problem/D

题意:给一个n个点,m条边的无向图。要求保留最多k条边,使得其他点到1点的最短路剩余最多。

思路:当找到单源最短路后,将其转换为一个所有点到点1都是最短路的树状结构,利用贪心确定所要保留的K条边(找离根最近的边,利用BFS)。

代码:

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
//#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 3e5 + ;
const int maxedge = 3e5 + ; struct qnode {
int v;
ll c;
qnode(int v=,ll c=) : v(v), c(c) {}
bool operator < (const qnode &r)const {//priority_queue 默认从大到小排列
return c > r.c;
}
}; struct Edge {
int v, w, pre;
} edge[maxedge*]; int point[maxn]; //point[i] = -1
int cnt;
bool vis[maxn]; // already init in the dijkstra
ll dist[maxn]; // already init in the dijkstra, overflow!
vector <int> v1[maxn];
vector <int> v2[maxn];
struct Path {
int u, e;
}path[maxn]; void AddEdge(int u, int v, int w) {
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].pre = point[u];
point[u] = cnt++;
} void Dijkstra(int n,int start) {
memset(vis,false,sizeof(vis));
memset(dist, 0x3f, sizeof(dist));
priority_queue <qnode> que;
while(!que.empty()) que.pop();
dist[start] = ;
que.push(qnode(start, ));
qnode tmp;
while(!que.empty()) {
tmp = que.top(); que.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i = point[u]; i != -; i = edge[i].pre) {
int v = edge[i].v;
int w = edge[i].w;
if(!vis[v] && dist[v]>dist[u]+w) {
dist[v] = dist[u]+w;
que.push(qnode(v, dist[v]));
path[v].u = u;
path[v].e = i;
}
}
}
} void init() {
cnt = ;
memset(point, -, sizeof(point));
} int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif int n, m, k;
scanf("%d%d%d", &n, &m, &k);
init();
for (int i = ; i < m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
AddEdge(u, v, w);
AddEdge(v, u, w);
}
Dijkstra(n, );
for (int i = ; i <= n; ++i) {
v1[path[i].u].push_back(i);
v2[path[i].u].push_back(path[i].e);
}
queue<int> q;
q.push();
int ans = ;
int Ans[maxn];
while (q.size()) {
int tmp = q.front();
q.pop();
for (int j = ; j < v1[tmp].size() && ans < k; ++j) {
q.push(v1[tmp][j]);
Ans[ans++] = v2[tmp][j]/;
}
}
printf("%d\n", ans);
for (int i = ; i < ans; ++i) {
if (i)
printf(" %d", Ans[i]+);
else printf("%d", Ans[i]+);
}
printf("\n");
#ifdef local
fclose(stdin);
#endif
return ;
}

Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion的更多相关文章

  1. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)

    题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...

  2. Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)

    第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...

  3. Educational Codeforces Round 54 (Rated for Div. 2) Solution

    A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...

  4. Educational Codeforces Round 54 (Rated for Div. 2) DE

    D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...

  5. Educational Codeforces Round 54 (Rated for Div. 2) ABCD

    A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...

  6. Educational Codeforces Round 96 (Rated for Div. 2) D. String Deletion (思维)

    题意:有一个\(01\)串,每次操作要先删除一个位置上的元素,然后删除相同前缀和,直到字符串被删完,问最多能操作多少次. 题解: 对于一个长度大于\(1\)的相同前缀,我们最多只能对它操作一次,然后就 ...

  7. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. iis设置http重置到https

    http://www.cnblogs.com/tangge/p/4259749.html 1.购买SSL证书,参考:http://www.cnblogs.com/yipu/p/3722135.html ...

  2. linux 普通用户批量创建账户与密码

    #!/bin/bash # add user imp - # by imp # ) do useradd imp$i echo "$i" |passwd --stdin imp$i ...

  3. 成功使Linux服务端和Windows客户端建立socket通信

    一.准备工作 1.一台装有虚拟机的Windows7操作系统,虚拟机中装的是CentOS6.5版本的Linux 2.Windows7已经装有java环境 二.编码 使用java编写socket通信的服务 ...

  4. LNMP(三)

    第二十二次课 LNMP(三) 目录 一.Nginx负载均衡 二.ssl原理 三.生成ssl密钥对 四.Nginx配置ssl 五.php-fpm的pool 六.php-fpm慢执行日志 七.open_b ...

  5. jQuery 的自定义事件

    jQuery  中,想要自动触发自定义事件,必须满足2个条件: 1.事件必须是通过on 来绑定的. 2.事件必须是通过trigger / triggerHandler 来触发. 格式如下: $(fun ...

  6. Hibernate中get()和load()方法区别

    get和load方式是根据id取得一个记录下边详细说一下get和load的不同,因为有些时候为了对比也会把find加进来. 1.从返回结果上对比:load方式检索不到的话会抛出org.hibernat ...

  7. ORACLE视图简单创建和使用

    1.创建 create view v (pdate,goodsId,productionId,qty)列名 as select p.pdate,p.goodsId,p.productionId,sum ...

  8. Charles篡改请求,在手机上抓包,以及弱网设置

    篡改请求 可以测试各种异常 原理:clint->server正常是客户端发送请求到服务端,charles相当于一个拦截器,拦住客户端的请求,并进行修改,修改后再发送到server端 Server ...

  9. FBV和CBV装饰器

    FBV装饰器: def cook(request): err_msg="" if request.method == "GET": return render( ...

  10. spyder常用功能

    最近和同学讨论到spyder的使用技巧,所以就结合之前在网上看到网友的总结( https://blog.csdn.net/peiwang245/article/details/78528098)和自己 ...