CF449B最短路

B. Jzzhu and Cities

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 mroads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. 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 si (and vise versa), the length of this route is yi.

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 ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).

Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ nui ≠ vi; 1 ≤ xi ≤ 109).

Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).

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.

Sample test(s)

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

题意:有n座城市m条边,现在可以修k条铁路,城市1是首都,如果到第i座城市的最短路比修到这座城市的铁路的距离要长,就修铁路,问你有多少条铁路是没必要修的。思路:我们可以首先把首都到每座城市的距离用首都到每座城市的铁路距离表示,然后再用dijkstra算法+优先队列跑一次所有最短路,看看那条最短路改变了,如果改变了,就标记这个顶点。由于有重边,所以在输入铁路的时候要去重,取最短距离的铁路,其他的重边都是没必要修的,由于有的点是没有铁路的,所以要标记那些有铁路的点,好最后计数。

代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
//typedef __int64 LL;
typedef long long LL;
const int maxn = 100009;
const int maxm = 300009;
const LL inf = (LL)1<<40;

struct node
{
    int v,w;
    node(int v=0,int w=0):v(v),w(w){}
    bool operator < (const node a)const
    {
        return w > a.w;
    }
};

vector<node> eg[maxn];
LL dis[maxn];
int n;
int vis[maxn];
int used[maxn];

void DIJ()
{
    int i;
    memset(used,0,sizeof(used));
    priority_queue<node> pq;
    dis[1] = 0;
    for(i=1;i<=n;i++)
        pq.push(node(i,dis[i]));
    while(!pq.empty())
    {
        int u = pq.top().v;
        int w = pq.top().w;
        pq.pop();
        if(w > dis[u])continue;
        for(int i=0;i<eg[u].size();i++)
        {
            int v = eg[u][i].v;
             w = eg[u][i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                used[v] = 1;   //
                pq.push(node(v,dis[v]));
            }else if(dis[v] == dis[u] + w)
                used[v] = 1;
        }

    }

}
inline int min(int a,int b){return a < b ? a : b;}

int main()
{
    int m,k,i,a,b,c;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(i=0;i<=n;i++)
        {
            dis[i] = inf;
            eg[i].clear();
        }
        memset(vis,0,sizeof(vis));
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            eg[a].push_back(node(b,c));
            eg[b].push_back(node(a,c));
        }
        int j = 0;
        int sum = 0;
        for(i=0;i<k;i++)
        {
            scanf("%d%d",&a,&b);
            if(!vis[a])
            {
                vis[a] = 1;
                dis[a] = b;
            }else
            {
                sum ++;
                dis[a] = min(dis[a],b);
            }
        }
        DIJ();
        for(i=1;i<=n;i++)
            if(vis[i] && used[i]) sum ++;
            printf("%d\n",sum);

    }
    return 0;
}

/*

    5 5 3
    1 2 1
    2 3 2
    1 3 3
    3 4 4
    1 5 5
    3 5
    4 5
    5 5
    5 4 3
    1 2 999999999
    2 3 1000000000
    3 4 529529529
    5 1 524524524
    5 524444444
    5 529999999
    2 1000000000
    2 2 3
    1 2 2
    2 1 3
    2 1
    2 2
    2 3

*/

CF449B最短路

时间: 2024-07-29 18:09:45

CF449B最短路的相关文章

hdu3461Marriage Match IV 最短路+最大流

//给一个图.给定起点和终点,仅仅能走图上的最短路 //问最多有多少种走的方法.每条路仅仅能走一次 //仅仅要将在最短路上的全部边的权值改为1.求一个最大流即可 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> using namespace std ; const int inf = 0x3f3f3f3f ; const

UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的T-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据. 每组数据第一行是两个整数NN ,MM (N≤100N≤100 ,M≤10000M≤1000

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

//转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define MAX 105 int lim, n; int p[M

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

hdu4725 拆点+最短路

题意:有 n 个点,每个点有它所在的层数,最多有 n 层,相邻两层之间的点可以互相到达,消耗 c (但同一层并不能直接到达),然后还有一些额外的路径,可以在两点间互相到达,并且消耗一定费用.问 1 点到 n 点的最小花费 将每一层拆成两个点,分别为进入层和出发层,然后相邻层的出发层可以指向进入层,花费 c,每个点可以到达其出发层,而进入层可以到达该点,花费 0 ,最后建立其余双向边,最短路 1 #include<stdio.h> 2 #include<string.h> 3 #in

hdu3416 最短路+最大流

题意:有 n 点 m 边,有出发点 A 到达点 B ,只允许走原图中的最短路,但每条边只允许被走一次,问最多能找出多少条边不重复的最短路 一开始做到的时候瞎做了一发最短路,WA了之后也知道显然不对,就放着了,后来打了今年的多校,再做到的时候发现和多校第一场的1007一样的……最短路+网络流就行了,只不过第一次做这个的时候我还不知道网络流是啥,不会做也正常啦. 首先对于原图跑一遍最短路求出每个点距离 A 点的最短路,然后对于每一条边,如果它的权值等于它连接的两点的最短路的差值的时候,就说明这条路是

【啊哈!算法】算法7:Dijkstra最短路算法

上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图中的1号顶点到2.3.4.5.6号顶点的最短路径. <ignore_js_op> 与Floyd-Warshall算法一样这里仍然使用二维数组e来存储顶点之间边的关系,初始值如下. <ignore_js_op> 我们还需要用一个一维数组dis来存储1号顶点到其余各个顶点的初始路程,如下.

HDU ACM 2544 最短路-&gt;最短路

最短路,简单题,floyd实现,在求最短路时一定要是是最大节点编号maxnum而不是输入的n,否则是错的. #include<iostream> using namespace std; int map[105][105]; //无向图 void Init() { int MAX=1000000,i,j; for(i=1;i<=104;i++) for(j=1;j<=104;j++) if(i==j) map[i][j]=0; else map[i][j]=MAX; } void