Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)
D. Jzzhu and Cities
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Jzzhu is the president of country A. There are \(n\) cities numbered from \(1\) to \(n\) in his country. City \(1\) is the capital of A. Also there are \(m\) roads connecting the cities. One can go from city \(u_i\) to \(v_i\) (and vise versa) using the \(i\)-th road, the length of this road is \(x_i\). Finally, there are \(k\) train routes in the country. One can use the \(i\)-th train route to go from capital of the country to city \(s_i\) (and vise versa), the length of this route is \(y_i\).
Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
Input
The first line contains three integers \(n, m, k (2 ≤ n ≤ 10^5; 1 ≤ m ≤ 3\cdot10^5; 1 ≤ k ≤ 10^5)\).
Each of the next m lines contains three integers \(u_i, v_i, x_i (1 ≤ u_i, v_i≤ n; u_i ≠ v_i; 1 ≤ x_i ≤ 10^9)\).
Each of the next k lines contains two integers \(s_i\) and \(y_i\) \((2 ≤ s_i ≤ n; 1 ≤ y_i ≤ 10^9)\).
It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output
Output a single integer representing the maximum number of the train routes which can be closed.
Examples
input
5 5 3
1 2 1
2 3 2
1 3 3
3 4 4
1 5 5
3 5
4 5
5 5
output
2
input
2 2 3
1 2 2
2 1 3
2 1
2 2
2 3
output
2
题意
一个城市中有 \(m\) 条公路和 \(k\) 条铁路,每条铁路都和起点相连。现在要求在不改变起点到各点最短路径长度的情况下,拆除一些铁路,问最多可以拆除多少条铁路
思路
将公路和铁路放在一起建图,然后去跑最短路,在跑最短路的过程中记录一下每个点的入度(该点被多少条最短路径包含)
铁路可以删除的条件:
- 如果起点到该点的最短路径和起点到该点的铁路长度相等,判断该点的入读是否大于 \(1\),如果大于 \(1\),这条铁路也是可以删除的(能够到达该点的最短路径不止一条)
 - 起点到该点的最短路径小于起点到该点的铁路的长度
 
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
struct edge
{
    int to,Next;
    ll value;
}Edge[maxn];
int head[maxn];
int tot;
inline void add_edge(int u,int v,ll w)
{
    Edge[tot].to=v;
    Edge[tot].Next=head[u];
    Edge[tot].value=w;
    head[u]=tot++;
}
struct node
{
    int u;
    ll d;
    bool operator < (const node & dui) const{return d>dui.d;}
};
int ss[maxn];
ll yy[maxn];
ll dis[maxn];
int in[maxn];
inline void dijkstra(int s)
{
    priority_queue<node>que;
    que.push(node{s,0});
    dis[s]=0;
    while(!que.empty())
    {
        node res=que.top();
        que.pop();
        int u=res.u;ll d=res.d;
        if(d!=dis[u])
            continue;
        for(int i=head[u];~i;i=Edge[i].Next)
        {
            int v=Edge[i].to;
            ll w=Edge[i].value;
            if(dis[v]==dis[u]+w)
                in[v]++;
            if(dis[v]>dis[u]+w)
                in[v]=1,dis[v]=dis[u]+w,que.push(node{v,dis[v]});
        }
    }
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("/home/wzy/in", "r", stdin);
        freopen("/home/wzy/out", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<=n;i++)
        dis[i]=INF;
    ms(head,-1);
    int x,y;
    ll z;
    while(m--)
        cin>>x>>y>>z,add_edge(x,y,z),add_edge(y,x,z);
    for(int i=0;i<k;i++)
        cin>>ss[i]>>yy[i],add_edge(1,ss[i],yy[i]),add_edge(ss[i],1,yy[i]);
    dijkstra(1);
    ll ans=0;
    for(int i=0;i<k;i++)
    {
        if(yy[i]==dis[ss[i]]&&in[ss[i]]>1)
            ans++,in[ss[i]]--;
        if(yy[i]>dis[ss[i]])
            ans++;
    }
    cout<<ans<<endl;
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s."<<endl;
    #endif
    return 0;
}
												
											Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)的更多相关文章
- Codeforces 450D Jzzhu and Cities [heap优化dij]
		
#include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...
 - Codeforces C. Jzzhu and Cities(dijkstra最短路)
		
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
 - Codeforces.567E.President and Roads(最短路 Dijkstra)
		
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...
 - Codeforces.1051F.The Shortest Statement(最短路Dijkstra)
		
题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...
 - Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)
		
I. Move Between Numbers time limit per test 2.0 s memory limit per test 256 MB input standard inpu ...
 - [Codeforces 449B] Jzzhu and Cities
		
[题目链接] https://codeforces.com/contest/449/problem/B [算法] 最短路 时间复杂度 : O(N ^ 2) [代码] #include<bits/ ...
 - codeforces 449B Jzzhu and Cities (Dij+堆优化)
		
输入一个无向图<V,E> V<=1e5, E<=3e5 现在另外给k条边(u=1,v=s[k],w=y[k]) 问在不影响从结点1出发到所有结点的最短路的前提下,最多可以 ...
 - Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造
		
原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...
 - Codeforces Round #257 (Div. 2)  D题:Jzzhu and Cities 删特殊边的最短路
		
D. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 
随机推荐
- mysql 间隙锁专题
			
本文研究记录mysql间隙锁,涉及以下情况 唯一索引 非唯一索引 范围更新 等值更新 mysql8 mysql7 RR RC 数据准备 mysql> select * from vodb.tes ...
 - GO Exit Fatal panic
			
Exit() 应用程序(不只是函数)退出执行 defer 不会被执行(因为程序都退出了) log.Fatal() 输出打印内容 应用程序退出 defer 不会被执行 panic() 函数停止执行(不是 ...
 - velocity示例
			
创建maven项目 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...
 - mysql读写分离(proxySQL)  lamp+proxysql+nfs
			
先在主从节点安装mysql [root@master-mariadb ~]# yum install mariadb-server -y [root@slave-mariadb ~]# yum ins ...
 - maven项目install时忽略执行test
			
1.在项目所在文件夹根目录使用maven命令打包时: <!-- 不执行单元测试,也不编译测试类 --> mvn install -Dmaven.test.skip=true 或 <! ...
 - 常用 HTTP 状态码
			
下面是列举的我在项目中用到过的一些 HTTP 状态码,当然,在具体的使用中并不是用到的状态码越多越好,需要结合自己项目情况来选用适合自己的 HTTP 状态码. HTTP 状态码 含义说明 200 ...
 - FastDFS的理解和分析
			
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...
 - Leetcode 78题-子集
			
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
 - pipeline option指令
			
目录 一.简介 二.参数 buildDiscarder checkoutToSubdirectory disableConcurrentBuilds newContainerPerStage retr ...
 - CTF靶场
			
CTF靶场测试报告 一.跨站脚本攻击(XSS) 实验原理:跨站脚本攻击( Cross Site Script),本来的缩写应为CSS,但是为了与层叠样式表(Cascading Style CSS)区分 ...