您的位置:首页 > 其它

[HDU 1142] A Walk Through the Forest

2017-08-13 14:35 429 查看
[align=left]Problem Description[/align]
Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and
Jimmy wants to take a different route everyday. He also wants to get home before
dark, so he always takes a path to make progress towards his house. He considers
taking a path from A to B to be progress if there exists a route from B to his
home that is shorter than any possible route from A. Calculate how many
different routes through the forest Jimmy might take.

大意:

给一张无向图,n 个点和 m 条边,cyb 在 1 号点,他要去 2 号点,

可以从 a 走到 b,当且仅当a到2的最短路,比b 到2的最短路长。

求 路径方案数

两条路径不同,当且仅当将两条路径中依次经过的边的编号不完全相同,

图可能会有重边;

[align=left]Input[/align]
Input contains several test cases followed by a line
containing 0. Jimmy has numbered each intersection or joining of paths starting
with 1. His office is numbered 1, and his house is numbered 2. The first line of
each test case gives the number of intersections N, 1 < N ≤ 1000, and the
number of paths M. The following M lines each contain a pair of intersections a
b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between
intersection a and a different intersection b. Jimmy may walk a path any
direction he chooses. There is at most one path between any pair of
intersections.

[align=left]Output[/align]
For each test case, output a single integer indicating
the number of different routes through the forest. You may assume that this
number does not exceed 2147483647

[align=left]Sample Input[/align]

5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1

7 5 1

6 7 1

5 2 1

6 2 1

0

[align=left]Sample Output[/align]

2
4
[b]预处理出所有点到2的距离[/b]
[b]第二次令f[i]是i点的方案数,f[v]+=f[u][/b]
[b]所以再来一次2开始的bfs,这次只有满足dist[u]<dist[v]才转移[/b]
[b]注意要拓扑序,否则会出现dist[v]还没算完就入队[/b]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lol;
struct Node
{
int next,to,dis;
}edge[200001];
int head[100001],num,q[1000001],dist[100001],n,m,in[100001];
lol ans[100001];
bool vis[100001];
void add(int u,int v,int d)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
edge[num].dis=d;
}
void spfa()
{int h,t,i;
q[1]=2;
h=0;t=1;
memset(dist,127/3,sizeof(dist));
dist[2]=0;
while (h<t)
{
h++;
h%=1000000;
int u=q[h];
vis[u]=0;
for (i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if (dist[v]>dist[u]+edge[i].dis)
{
dist[v]=dist[u]+edge[i].dis;
if (vis[v]==0)
{
t++;
t%=1000000;
q[t]=v;
vis[v]=1;
}
}
}
}
}
int main()
{int i,j,u,v,d,h,t;
//freopen("mod.in","r",stdin);
//freopen("mod.out","w",stdout);
while (cin>>n>>m&&n&&m)
{num=0;
memset(head,0,sizeof(head));
memset(edge,0,sizeof(edge));
memset(ans,0,sizeof(ans));
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&d);
add(u,v,d);
add(v,u,d);
}
spfa();
for (i=1;i<=n;i++)
for (j=head[i];j;j=edge[j].next)
if (dist[edge[j].to]>dist[i])
in[edge[j].to]++;
//    for (i=1;i<=n;i++)
//    cout<<dist[i]<<endl;
q[1]=2;
h=0;t=1;
memset(vis,0,sizeof(vis));
ans[2]=1;
while (h<t)
{
h++;
h%=1000000;
int u=q[h];
for (i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if (dist[v]>dist[u])
{in[v]--;
ans[v]=(ans[v]+ans[u]);
if (in[v]==0)
{
t++;
t%=1000000;
q[t]=v;
}
}

}
}
cout<<ans[1]<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: