您的位置:首页 > 其它

UVA10917 Walk Through the Forest

2017-04-10 22:22 323 查看
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.

Input

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.

Output

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.

Sample Input

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

Sample Output

2

4

题目大意:一个图,1为起点,2为终点,只能走a——>b,a,b满足b到2的最短距离小于a到2的最短距离,求路线条数。

解题思路:先从2跑一遍Dijkstra,得到各点到2的最短距离。然后从1开始dp,对u的邻接点v,如果dist[v]

/*
使用优先队列Dijkstra算法
复杂度O(ElogE)
注意对vector<Edge> E[MAXN]进行初始化后加边
*/

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<fstream>
#include<string.h>
using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN=1010;

struct qnode
{
int v;
int c;
qnode(int _v=0,int _c=0):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};

struct Edge
{
int v,cost;
Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};

vector<Edge> E[MAXN];
vector<int> New[MAXN];
bool vis[MAXN];
int dist[MAXN];

void Dijkstra(int n,int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++) dist[i]=INF;
priority_queue<qnode> que;
while(!que.empty()) que.pop();
dist[start]=0;
que.push(qnode(start,0));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u]) continue;
vis[u]=true;
for(int i=0;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}

void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}

void init()
{
for(int i=0;i<=MAXN;i++)
if(!E[i].empty())
E[i].clear();
}

int dp[1010];
int solve(int u)
{
if(dp[u]!=-1) return dp[u];
else if(u==2) return 1;
else
{
dp[u]=0;
int sz=E[u].size();
for(int i=0;i<sz;i++)
{
if(dist[E[u][i].v]<dist[u])
dp[u]+=solve(E[u][i].v);
}
}
return dp[u];
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,e;
while(cin>>n)
{
if(n==0) break;
cin>>e;
init();
int u,v,w;
for(int i=1;i<=e;i++)
{
cin>>u>>v>>w;
addedge(u,v,w);
addedge(v,u,w);
}
Dijkstra(n,2);
memset(dp,-1,sizeof(dp));
cout<<solve(1)<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: