最短路计数(Shortest Path Count)

By Stockholm

最短路计数(Shortest Path Count)

来源:Luogu P1144
题目描述
给出一个 N 个顶点 M 条边的无向无权图,顶点编号为 1~N。问从
顶点 1 开始,到其他每个点的最短路有几条。
输入输出格式
输入格式:
输入第一行包含 2 个正整数 N,M,为图的顶点数与边数。
接下来 M 行,每行两个正整数 x, y,表示有一条顶点 x 连向顶点 y
的边,请注意可能有自环与重边。
输出格式:
输出包括 N 行,每行一个非负整数,第 i 行输出从顶点 1 到顶点 i 有
多少条不同的最短路,由于答案有可能会很大,你只需要输出 mod
100003 后的结果即可。如果无法到达顶点 i 则输出 0。输入输出样例
输入样例#1:
5 7
1 2
1 3
2 4
3 4
2 3
4 5
4 5
输出样例#1:
1 1 1 2 4
说明1 到 5 的最短路有 4 条, 分别为 2 条 1-2-4-5 和 2 条 1-3-4-5 (由于 4-5的边有 2 条)。
对于 20%的数据,N ≤ 100;
对于 60%的数据,N ≤ 1000;
对于 100%的数据,N<=1000000,M<=2000000。

思路

这个题用 SPFA 做,但是需要加一个计数操作,注意最后结果取模,以及如果一样的长度, 就需要在原来的基础上加上上一个节点累加器
中的记录数,SPFA 最好带上 SLF,这样快一些。

代码(C++)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<deque>
using namespace std;
int i,j,m,n;
int head[1000001];
int us[1000001];
int t[1000001];int dis[1000001];
struct node
{
int yy;
struct node *nxt;
}a[4000005];
int r()
{
int aans=0;
char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
{
aans*=10;
aans+=ch-'0';
ch=getchar();
}
return aans;
}int spfa(int x)
{
us[x]=1;
deque<int>q;
dis[x]=0;
q.push_front(x);
struct node *p;
while(!q.empty())
{
x=q.front();
p=&a[head[x]];
us[x]=0;
q.pop_front();
while(p->yy!=0)
{
if(dis[x]+1<dis[p->yy])
{
t[p->yy]=t[x]%100003;
dis[p->yy]=dis[x]+1;if(!us[p->yy])
{
if(q.empty()||dis[q.front()]>dis[p->yy])
q.push_front(p->yy);
else
q.push_back(p->yy);
us[p->yy]=1;
}
}
else if(dis[x]+1==dis[p->yy])
{t[p->yy]+=t[x];
t[p->yy]%=100003;}
p=p->nxt;
}
}
}
int main()
{
n=r(),m=r();
int x,y;for(i=1;i<=m;i++)
{
x=r(),y=r();
a[2*i-1].yy=y;
a[2*i-1].nxt=&a[head[x]];
head[x]=2*i-1;
a[2*i].yy=x;
a[2*i].nxt=&a[head[y]];
head[y]=2*i;
}
memset(dis,0x7f7f7f7f,sizeof(dis));
t[1]=1;
spfa(1);
for(i=1;i<=n;i++)
if(dis[i]!=0x7f7f7f7f)
cout<<t[i]<<endl;
else
cout<<0<<endl;
return 0;
}

这里写图片描述

One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci the father somehow knows it and wants to stop her. There are N N spots in the jail and M M roads connecting some of the spots. JOJO finds that Pucci knows the route of the former ( K − 1 ) (K−1)-th shortest path. If Pucci spots JOJO in one of these K − 1 K−1 routes, Pucci will use his stand Whitesnake and put the disk into JOJO's body, which means JOJO won't be able to make it to the destination. So, JOJO needs to take the K K-th quickest path to get to the destination. What's more, JOJO only has T T units of time, so she needs to hurry. JOJO starts from spot S S, and the destination is numbered E E. It is possible that JOJO's path contains any spot more than one time. Please tell JOJO whether she can make arrive at the destination using no more than T T units of time. Input There are at most 50 50 test cases. The first line contains two integers N N and M M ( 1 ≤ N ≤ 1000 , 0 ≤ M ≤ 10000 ) (1≤N≤1000,0≤M≤10000). Stations are numbered from 1 1 to N N. The second line contains four numbers S , E , K S,E,K and T T ( 1 ≤ S , E ≤ N 1≤S,E≤N, S ≠ E S  =E, 1 ≤ K ≤ 10000 1≤K≤10000, 1 ≤ T ≤ 100000000 1≤T≤100000000 ). Then M M lines follows, each line containing three numbers U , V U,V and W W ( 1 ≤ U , V ≤ N , 1 ≤ W ≤ 1000 ) (1≤U,V≤N,1≤W≤1000) . It shows that there is a directed road from U U-th spot to V V-th spot with time W W. It is guaranteed that for any two spots there will be only one directed road from spot A A to spot B B ( 1 ≤ A , B ≤ N , A ≠ B ) (1≤A,B≤N,A  =B), but it is possible that both directed road a n d d i r e c t e d r o a d anddirectedroad exist. All the test cases are generated randomly. Output One line containing a sentence. If it is possible for JOJO to arrive at the destination in time, output "yareyaredawa" (without quote), else output "Whitesnake!" (without quote).的c++代码
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值