Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion
题目链接: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的更多相关文章
- Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)
题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...
- Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)
第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...
- Educational Codeforces Round 54 (Rated for Div. 2) Solution
A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...
- Educational Codeforces Round 54 (Rated for Div. 2) DE
D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...
- 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 ...
- Educational Codeforces Round 96 (Rated for Div. 2) D. String Deletion (思维)
题意:有一个\(01\)串,每次操作要先删除一个位置上的元素,然后删除相同前缀和,直到字符串被删完,问最多能操作多少次. 题解: 对于一个长度大于\(1\)的相同前缀,我们最多只能对它操作一次,然后就 ...
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- 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 ...
- 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 ...
随机推荐
- Python语言:Day11练习题
24.实现一个整数加法计算器:如:content=input('请输入内容:')#如:5+9或5+ 9或5 + 9 content = input("请输入需要计算的:") #如: ...
- ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5
环境: - ubuntu 16.04 - OpenCV + contrib 3.2.0 (文中附下载链接) - Python 3.5 基于其他环境的配置应该大同小异. 没时间解释了,直接上车. 更新下 ...
- centos7初上手3-安装apache服务
前两篇学习安装了mysql服务器,tomcat服务,这篇文章学习安装apache服务 1.执行yum install httpd,安装完成后查看httpd rpm -qa|grep httpd 2.新 ...
- linux - whatis 提示 ls: nothing appropriate
linux 执行命令 whatis 时,提示: ls: nothing appropriate 原因:whatis是在数据库中执行查找操作,这个数据库是定期更新的, 新安装的系统,系统没有更新,没有这 ...
- win10安装pytorch
安装gpu版本的pytorch需要三个东西:pytorch(torchvision).cuda.cudnn 相信大家都安装过了anaconda,就不介绍anaconda的安装了 1.安装cuda:从官 ...
- Traceur
直接插入网页 <!-- 加载Traceur编译器 --> <script src="http://google.github.io/traceur-compiler/bin ...
- 字符串a-b
#include<iostream> #include<stdio.h> #include<algorithm> #include<cmath> #in ...
- shell版的nginx安装
#!/bin/bash # Name:Centos 6.4 安装nginx1.8.1 # Date:-- # Author:qifei@meizu.com Home=$(cd ``;pwd) #这个命 ...
- Python Django install Error
Exception:Traceback (most recent call last): File "/home/djangogirls/myvenv/lib/python3.6/site ...
- PostMan Test 的脚本scripts编写方法
设置环境变量 pm.environment.set("variable_key", "variable_value"); 将一个嵌套的对象设置为一个环境变量 v ...