Codeforces 449B Jzzhu and Cities【最短路SPFA+思维+玄学优先队列】
2016-08-15 12:32
369 查看
B. 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 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 ≤ n; ui ≠ 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.
Examples
input
output
input
output
题目大意:
有n个点,m条无向边,k条火车路。
火车路是从节点1直接通向节点的路。
问最多可以拆除多少条火车路。
思路:
1、首先,建图之后跑最短路 ,得到从节点1到其他个点的最短路。
2、然后将dis【i】<火车路花费的火车路拆除掉,因为显然这条火车路是不需要的,因为有更短的路径比火车路的路径更短
3、当dis【i】==火车路花费的时候,我们要记录节点i的最短路径入度,如果入度>1,那么就可以拆除当前火车路。因为这样就说明有两条及以上的路径能够从1到达节点i,那么这个火车路也是多余的。那么要如何记录节点i的最短路径入度呢?我们在松弛的时候加上两个语句:
if(dis【v】>dis【u】+w)in【v】=1;
if(dis【v】==dis【u】+w)in【v】++;
这样我们就能够统计下来节点i的最短路径条数。
4、玄学优先队列。我写的是SPFA,队列TLE on 45,栈TLE on 4,因为比较渣,堆写的很挫,写了发优先队列+Dij,也是TLE on45,最后百度了一下题解,竟然可以用优先队列水过数据.........................(难道优先队列真的能够优化算法?)
Ac代码;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
ll head[1000000];
struct node
{
ll from;
ll to;
ll next;
ll w;
}e[1515151];
ll in[15151515];
ll go[1515151];
ll val[1515151];
ll dis[1515155];
ll vis[1515155];
ll n,m,k,cont;
void add(ll from,ll to,ll w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA(ll ss)
{
priority_queue<ll>s;
for(ll i=1;i<=n;i++)dis[i]=1000000000000000000;
memset(vis,0,sizeof(vis));
vis[ss]=1;
dis[ss]=0;
s.push(ss);
while(!s.empty())
{
ll u=s.top();
s.pop();
vis[u]=0;
for(ll i=head[u];i!=-1;i=e[i].next)
{
ll v=e[i].to;
ll w=e[i].w;
if(dis[v]>dis[u]+w)
{
in[v]=1;
dis[v]=dis[u]+w;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
else if(dis[v]==dis[u]+w)in[v]++;
}
}
}
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
{
cont=0;
memset(in,0,sizeof(in));
memset(head,-1,sizeof(head));
for(ll i=0;i<m;i++)
{
ll x,y,w;
scanf("%I64d%I64d%I64d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
for(ll i=0;i<k;i++)
{
scanf("%I64d%I64d",&go[i],&val[i]);
add(1,go[i],val[i]);
add(go[i],1,val[i]);
}
ll output=0;
SPFA(1);
for(ll i=0;i<k;i++)
{
if(dis[go[i]]<val[i])output++;
else if(dis[go[i]]==val[i])
{
if(in[go[i]]>1)
{
in[go[i]]--;
output++;
}
}
}
printf("%I64d\n",output);
}
}
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 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 ≤ n; ui ≠ 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.
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 22 1 3
2 1
2 22 3
output
2
题目大意:
有n个点,m条无向边,k条火车路。
火车路是从节点1直接通向节点的路。
问最多可以拆除多少条火车路。
思路:
1、首先,建图之后跑最短路 ,得到从节点1到其他个点的最短路。
2、然后将dis【i】<火车路花费的火车路拆除掉,因为显然这条火车路是不需要的,因为有更短的路径比火车路的路径更短
3、当dis【i】==火车路花费的时候,我们要记录节点i的最短路径入度,如果入度>1,那么就可以拆除当前火车路。因为这样就说明有两条及以上的路径能够从1到达节点i,那么这个火车路也是多余的。那么要如何记录节点i的最短路径入度呢?我们在松弛的时候加上两个语句:
if(dis【v】>dis【u】+w)in【v】=1;
if(dis【v】==dis【u】+w)in【v】++;
这样我们就能够统计下来节点i的最短路径条数。
4、玄学优先队列。我写的是SPFA,队列TLE on 45,栈TLE on 4,因为比较渣,堆写的很挫,写了发优先队列+Dij,也是TLE on45,最后百度了一下题解,竟然可以用优先队列水过数据.........................(难道优先队列真的能够优化算法?)
Ac代码;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
ll head[1000000];
struct node
{
ll from;
ll to;
ll next;
ll w;
}e[1515151];
ll in[15151515];
ll go[1515151];
ll val[1515151];
ll dis[1515155];
ll vis[1515155];
ll n,m,k,cont;
void add(ll from,ll to,ll w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA(ll ss)
{
priority_queue<ll>s;
for(ll i=1;i<=n;i++)dis[i]=1000000000000000000;
memset(vis,0,sizeof(vis));
vis[ss]=1;
dis[ss]=0;
s.push(ss);
while(!s.empty())
{
ll u=s.top();
s.pop();
vis[u]=0;
for(ll i=head[u];i!=-1;i=e[i].next)
{
ll v=e[i].to;
ll w=e[i].w;
if(dis[v]>dis[u]+w)
{
in[v]=1;
dis[v]=dis[u]+w;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
else if(dis[v]==dis[u]+w)in[v]++;
}
}
}
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
{
cont=0;
memset(in,0,sizeof(in));
memset(head,-1,sizeof(head));
for(ll i=0;i<m;i++)
{
ll x,y,w;
scanf("%I64d%I64d%I64d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
for(ll i=0;i<k;i++)
{
scanf("%I64d%I64d",&go[i],&val[i]);
add(1,go[i],val[i]);
add(go[i],1,val[i]);
}
ll output=0;
SPFA(1);
for(ll i=0;i<k;i++)
{
if(dis[go[i]]<val[i])output++;
else if(dis[go[i]]==val[i])
{
if(in[go[i]]>1)
{
in[go[i]]--;
output++;
}
}
}
printf("%I64d\n",output);
}
}
相关文章推荐
- Codeforces 128B. String(优先队列,模仿dijkstra求最短路)
- Interesting Calculator (最短路,广搜+优先队列,dp)(简单题)
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- #Codeforces 381 [div2] D. Alyona and a tree 【树链剖分 + 优先队列】
- #Codeforces 343 [div2] D. Babaei and Birthday Cake 【线段树优先队列查询优化】
- Codeforces 845 C Two TVs(贪心之优先队列维护)
- 最短路&&优先队列优化->HDU2544
- hdu 2544 最短路(优先队列 迪杰斯特拉Dijkstr)
- hdu 2066最短路 dijkstra 及其优先队列优化
- Codeforces 426C - Sereja and Swaps 【优先队列||DP】
- dijkstra 优先队列最短路模板
- Codeforces 767E Change-free【贪心+优先队列】
- poj3463 hdu1688 次短路+优先队列 如果让我说:我只能说,实力决定一切
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
- HDOJ 4318 —— dijkstra + 优先队列 求最短路
- 广度优先搜索以及队列优化的 Bellman-Ford 短路算法
- 笔记③:POJ Roadblocks 次短路问题代码解析(优先队列逆序排列两种方法)
- codeforces 731E (优先队列 DP)
- Codeforces 913D - Too Easy Problems 【优先队列】
- 【51Nod】1459 - 迷宫游戏(bfs & 优先队列 & 最短路)