题意: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. php学习笔记:利用gd库生成图片,并实现随机验证码

    说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...

  2. 六个创建模式之简单工厂模式(Simple Factory Pattern)

    定义: 定义一个工厂类,它可以根据参数的不同生成对应的类的实例:被创建的类的实例通常有相同的父类.因为该工厂方法尝尝是静态的,所以又被称为静态工厂方法(Static Factory Method) 结 ...

  3. 关于HTML的编码问题

    平时我在写html文件时,很容易忘掉这个文件的编码类型,<meta charset=”utf-8”> 的语句,因为编辑器默认设置了一个编码,所以在我没有写编码格式设置语句的情况下,效果依然 ...

  4. Threading.Tasks 简单的使用

    using Lemon.Common; using System; using System.Collections.Generic; using System.Linq; using System. ...

  5. SharePoint 2013 使用查阅项实现联动下拉框

    SharePoint列表使用中,经常会用到下拉框,而有些特殊的需求,会用到联动的下拉框,在SharePoint中默认没有这样的字段,所以如果实现,我们需要自己想办法. 这里,我们介绍如何使用JQuer ...

  6. 3.0之后在LinearLayout里增加分割线

    android:divider="@drawable/shape"<!--分割线图片--> android:showDividers="middle|begi ...

  7. 关于Activity销毁,而绘制UI的子线程未销毁出现的问题

    项目总结 ----------------------------------------------------------------------------------------------- ...

  8. [Android]IllegalStateException: Could not find method onBind(View)

    FATAL EXCEPTION: main Process: org.diql.aidldemo, PID: 2274 java.lang.IllegalStateException: Could n ...

  9. 如何发布得到.ipa文件

    第一个方法: 如果都有证书的话,并且又不想把别人的机器添加到测试设备中,或者感觉获取UDID麻烦的话,那么就可以采用该方法了. 直接Archive应用程序: 右键显示包内容到product下复制里面的 ...

  10. 集成ZBar时容易遇到的问题以及解决方法

    1.添加入几个必备的框架: libiconv.tbd QuartzCore.framework CoreVideo.framework CoreMedia.framework AVFoundation ...