Codeforces Round #257 (Div. 1) (Codeforces 449B)
题意:给力一张无向图,有一些边是正常道路,有一些边是铁路,问最多能删除几条铁路使得所有点到首都(编号为1)的最短路长度不变。
思路:求不能删除的铁路数,总数减掉就是答案。先求出首都到所有点的最短路,求完最短路后,枚举除首都外所有点,如果这个点被更新的边中只有铁路,那么就有一条铁路不能删除。
注意:这里求最短路一开始用SPFA在第45个点TLE,最后换成带堆优化Dijkstra
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include <iostream>
#include<queue>
#define M 1000010
#define N 100050
#define LL __int64
#define INF (1000000000000000LL)
using namespace std;
struct node {
int to, nx, va, flag;
} e[M];
int head[N], ecnt;
struct info {
int id, va;
};
bool operator<(info a, info b) {
return a.va > b.va;
}
priority_queue<info> q;
void addedge(int x, int y, int va, int flag) {
e[ecnt].to = y;
e[ecnt].va = va;
e[ecnt].nx = head[x];
e[ecnt].flag = flag;
head[x] = ecnt++;
}
bool bo[N];
LL d[N];
int n, m, k;
void Dij() {
for (int i = ; i <= n; ++i)
d[i] = INF;
memset(bo, , sizeof(bo));
d[] = ;
int st = , ed = ;
while (!q.empty())
q.pop();
info a;
a.va = ;
a.id = ;
q.push(a);
while (!q.empty()) {
a = q.top();
q.pop();
int now = a.id;
if (bo[now])
continue;
bo[now] = true;
for (int j = head[now]; j != -; j = e[j].nx) {
int u = e[j].to;
if (d[u] > d[now] + e[j].va) {
d[u] = d[now] + e[j].va;
a.id=u;
a.va=d[u];
q.push(a);
}
}
}
}
void init() {
scanf("%d%d%d", &n, &m, &k);
memset(head, -, sizeof(head));
ecnt = ;
for (int i = ; i < m; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z, );
addedge(y, x, z, );
}
for (int i = ; i < k; ++i) {
int x, y;
scanf("%d%d", &x, &y);
addedge(, x, y, );
addedge(x, , y, );
}
}
void solve() {
Dij();
int ans = k;
for (int i = ; i <= n; ++i) {
int flag = , flag1 = ;
for (int j = head[i]; j != -; j = e[j].nx) {
int u = e[j].to;
if (d[u] + e[j].va == d[i]) {
if (e[j].flag == )
flag = ;
else
flag1 = ;
}
}
if (flag == && flag1 == )
ans--;
}
printf("%d\n", ans);
}
int main() {
init();
solve(); }
Codeforces Round #257 (Div. 1) (Codeforces 449B)的更多相关文章
- Codeforces Round #257 (Div. 1) (Codeforces 449D)
思路:定义f(x)为 Ai & x==x 的个数,g(x)为x表示为二进制时1的个数,最后答案为 .为什么会等于这个呢:运用容斥的思想,如果 我们假设 ai&x==x 有f(x ...
- Codeforces Round #257 (Div. 1)A~C(DIV.2-C~E)题解
今天老师(orz sansirowaltz)让我们做了很久之前的一场Codeforces Round #257 (Div. 1),这里给出A~C的题解,对应DIV2的C~E. A.Jzzhu and ...
- Codeforces Round #524 (Div. 2) codeforces 1080A~1080F
目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)
题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...
- Codeforces Round #257 (Div. 2)
A - Jzzhu and Children 找到最大的ceil(ai/m)即可 #include <iostream> #include <cmath> using name ...
随机推荐
- 06 SQL执行计划
解释计划 与 执行计划的 区别 随着可以得到解释计划输出的开发工具, 比如 toad 的普遍使用, 生成解释计划就变的相当简单. 而不简单的是得到执行计划. 解释计划 EXPLAIN PLAN 用来显 ...
- 用CSS样式截取字符串,多的用省略号表示
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- css+JS实现遮罩弹框
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...
- Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要
Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...
- Android开发面试经——1.常见人事面试问题
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 做为程序员,我们都是有梦想的人,有时候当我 ...
- error C2783: 无法为“T”推导 模板 参数
原则:“模板参数推导机制无法推导函数的返回值类型” 版本一: // 缺少<T> 参数 int n 对比第三个版本( 缺少<T> 参数 T n) ! 编译错误提示: 错误 1 e ...
- PRIMARY LANGUAGE ID not a number
用vs2010修改别人的源代码(估计是vc6下的) .RC 文件,报错: 1>.RC(8): error RC2144: PRIMARY LANGUAGE ID not a number 1 ...
- 能源项目xml文件 -- springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- python 练习 13
#!/usr/bin/python # -*- coding: UTF-8 -*- l = [] for i in range(3): x = int(raw_input('integer:\n')) ...
- ThreadLocal 那点事儿
原文出处: 黄勇 ThreadLocal,直译为“线程本地”或“本地线程”,如果你真的这么认为,那就错了!其实,它就是一个容器,用于存放线程的局部变量,我认为应该叫做 ThreadLocalVaria ...