题意:给力一张无向图,有一些边是正常道路,有一些边是铁路,问最多能删除几条铁路使得所有点到首都(编号为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)的更多相关文章

  1. Codeforces Round #257 (Div. 1) (Codeforces 449D)

    思路:定义f(x)为 Ai & x==x  的个数,g(x)为x表示为二进制时1的个数,最后答案为    .为什么会等于这个呢:运用容斥的思想,如果 我们假设 ai&x==x 有f(x ...

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

  3. Codeforces Round #524 (Div. 2) codeforces 1080A~1080F

    目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...

  4. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  5. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  6. Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)

    主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...

  7. Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

    题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...

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

  9. Codeforces Round #257 (Div. 2)

    A - Jzzhu and Children 找到最大的ceil(ai/m)即可 #include <iostream> #include <cmath> using name ...

随机推荐

  1. JavaWeb学习总结(五)—Myecplise的优化

    1.设定项目的默认编码 General --> Workspace --> UTF-8 2.设定JSP的打开方式,默认会很占内存 General --> Editors --> ...

  2. (三)uboot源码分析

    一.九鼎官方uboot和三星原版uboot对比(1)以九鼎官方的uboot为蓝本来学习的,以三星官方的这份为对照.(2)不同版本的uboot或者同一版本不同人移植的uboot,可能目录结构和文件内容都 ...

  3. Volley框架的流程图分析

          接着上一篇Volley框架的使用,这一篇主要主要讲Volley框架运作的原理.主要使用流程图来叙述,简单的分析了整个流程的过程,具体的请参考源代码或者查看我上一篇在文章末尾添上的链接. 一 ...

  4. [转]c++面向对象基础

    from: here 1. C++面向对象程序设计的重要概念 早期革命影片里有这样一个角色,他说:“我是党代表,我代表党,我就是党.”后来他给同志们带来了灾难. 会用C++的程序员一定懂得面向对象程序 ...

  5. intel simd 资料

    http://www.cnblogs.com/zyl910/archive/2012/04/26/md00.html https://software.intel.com/sites/landingp ...

  6. git reset 理解

    http://www.open-open.com/lib/view/open1397013992747.html 一般在工作中用的比较多的是: git reset --hard <commitI ...

  7. Structs1 -配置例子(转)

    转自:(http://blog.csdn.net/xys_777/article/details/7542095) Action, ActionForm, ActionForward ,这三个对象构成 ...

  8. SSH2 架构常用注解

    1. @Repository 将 DAO 类声明为 Bean 2.@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次. 3.@Service 通常作用在业务层 ...

  9. VC++编译MPIR 2.7.0

    目录 第1章编译    2 1.1 简介    2 1.2 下载    3 1.3 解决方案    4 1.4 创建项目    5 1.5 复制文件树    6 1.6 不使用预编译头文件    8 ...

  10. mysql通过binlog日志来恢复数据

    简介 在生产的过程中有这么一个业务场景:比如我在2016-11-19 09:30:00 通过mysqldump的方式备份了数据库,但是在2016-11-19 10:30:00的时候数据库崩溃了,如果通 ...