题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路

分析:先求一次最短路,铁路的权值大于该点最短距离的显然可以删去,否则将该条边加入图中,再求最短路,记录每个点的前一个点,然后又枚举铁路,已经删去的就不用处理了,如果铁路权值大于该点最短距离又可以删去,权值相等时,该点的前一个点如果不为1,则这个点可以由其他路到达,这条铁路又可以删去。

由于本题中边比较多,最多可以有8x10^5条,所以用邻接链表会超时,最好用vector来存储边。

Time:296 ms Memory:30968 KB

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#define lll __int64
using namespace std;
#define N 100006 struct Edge
{
int v;
lll w;
Edge(int _v,lll _w)
{
v = _v;
w = _w;
}
Edge(){}
}; struct Train
{
int v;
lll w;
}T[N]; vector<Edge> G[N];
lll dis[N];
int n,m;
int head[N],tot,pre[N];
int cut[N],vis[N];
queue<int> que; void SPFA(int s)
{
while(!que.empty())
que.pop();
memset(vis,,sizeof(vis));
vis[s] = ;
que.push(s);
for(int i=;i<=n;i++)
dis[i] = 1000000000000000LL;
dis[s] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].v;
lll w = G[u][i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!vis[v])
{
vis[v] = ;
que.push(v);
}
}
}
}
} void SPFA2()
{
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].v;
lll w = G[u][i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
pre[v] = u;
if(!vis[v])
{
que.push(v);
vis[v] = ;
}
}
else if(dis[v] == dis[u] + w && pre[v] < u)
pre[v] = u;
}
}
} int main()
{
int i,j,k;
int u,v,y;
lll w;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
tot = ;
for(i=;i<=n;i++)
G[i].clear();
memset(head,-,sizeof(head));
memset(cut,,sizeof(cut));
memset(pre,-,sizeof(pre));
for(i=;i<m;i++)
{
scanf("%d%d%I64d",&u,&v,&w);
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
for(i=;i<k;i++)
{
scanf("%d%I64d",&y,&w);
T[i].v = y;
T[i].w = w;
}
SPFA();
int cnt = ;
for(i=;i<k;i++)
{
int v = T[i].v;
lll w = T[i].w;
if(dis[v] <= w)
{
cut[i] = ;
cnt++;
}
else
{
G[].push_back(Edge(v,w));
G[v].push_back(Edge(,w));
pre[v] = ;
dis[v] = w;
que.push(v);
vis[v] = ;
}
}
SPFA2();
for(i=;i<k;i++)
{
if(cut[i])
continue;
int v = T[i].v;
lll w = T[i].w;
if((dis[v] == w && pre[v] != ) || dis[v] < w)
cnt++;
}
printf("%d\n",cnt);
}
return ;
}

Codeforces Round #257(Div.2) D Jzzhu and Cities --SPFA的更多相关文章

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

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

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

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

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

  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. 2) B Jzzhu and Sequences

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  7. Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp

    D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...

  8. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate

    C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #257 (Div. 2) A. Jzzhu and Children

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. custom struts framework

    1. Difference between stucts1 and struts2 struts1 : Servlet used as Controller , you can visit the S ...

  2. Web网站数据”实时”更新设计

    请注意这个实时打上了双引号,没有绝对的实时,只是时间的颗粒不一样罢了(1ms,1s,1m). 服务器数据有更新可以快速通知客户端.Web 基于取得模式,而服务器建立大量的和客户端连接来提供数据实时更新 ...

  3. SystemClock.sleep和Thread.sleep的区别(转)

    在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方 ...

  4. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance

    http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...

  5. SharePoint 2013 新功能探索 之 列表等级设置

    一.列表等级及赞功能 ,在SharePoint 2010 中,对列表的等级设定,需要一定时间才能看到,现在可以实时同步,评分人数也能显示出来 等级分为两类 赞和星级评定   

  6. 个人总结 HTML+CSS

    从大一下学期接触,一直到今年,接触的时间也挺长的了,最近一些认识的盆友和同学说是想学习前端,自己也开始慢慢停下脚步,不再拼命地去学很多框架的东西,回归到基础,慢慢把基础打牢 很多知识碎片一直来不及整理 ...

  7. A除以B问题

    描述:本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入:输入在1行中依次给出A和B,中间以1空格分隔. 输出: ...

  8. Sass学习之路(1)——Sass简介

    Sass是CSS的一种预处理器语言,类似的语言还有Less,Stylus等. 那么什么是CSS预处理器? CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些 ...

  9. miniSipServer简单而不简单,轻松落地,实现电脑对固话、手机通讯

    最近沉迷于SIP通讯,网内通讯全免费,落地也就几分钱,而且无漫游全国拨打,想想真是心动呢,只要有网落就ok!. 对于sipserver,现在的市场上软件很多,免费的.收费的应有尽有,这里不一一例举.综 ...

  10. Mybatis学习记录(六)----Mybatis的高级映射

    1.一对多查询 1.1 需求 查询订单及订单明细的信息. 1.2 sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT orders. ...