HDU

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.

InputInput 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.
OutputFor 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有多少种走法,满足每一步到的点的最短路递减。

思路:反向求最短路,然后就是一个拓扑图,然后拓扑就好了(貌似DFS也能过,毕竟是单源起点)。

如果要拓扑怎么搞呢,如果我不管1的入度是否为0,一开始就把1点放进去,就不太好搞。  事实上,还是应该像普通的拓扑那样写,只是除了1号点初始值为1,其他都为0;   是我傻逼了。。。。下周出给学弟们,看看有人和我一样的没。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int inf=2147483647;
int Laxt[maxn],Next[maxn*maxn],To[maxn*maxn],ind[maxn],N,vis[maxn];
int dp[maxn],Len[maxn*maxn],From[maxn*maxn],cnt,dis[maxn];
struct in{
    int dis,id;
    friend bool operator<(in w,in v){ return w.dis>v.dis; }
};
vector<int>G[maxn];
void add(int u,int v,int w){
    Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
    Len[cnt]=w; From[cnt]=u;
}
void SPFA()
{
    for(int i=1;i<=N;i++) dis[i]=inf; dis[2]=0;
    priority_queue<in>q; q.push(in{0,2});
    while(!q.empty()){
        int u=q.top().id; q.pop();
        for(int i=Laxt[u];i;i=Next[i]){
            int v=To[i];
            if(dis[u]+Len[i]<dis[v]){
                dis[v]=dis[u]+Len[i]; q.push(in{dis[v],v});
            }
        }
    }
}
int main() {
    int M,u,v,w;
    while(~scanf("%d",&N)){
        if(N==0) break; cnt=0; scanf("%d",&M);
        for(int i=1;i<=N;i++) Laxt[i]=dp[i]=ind[i]=vis[i]=0,G[i].clear();
        for(int i=1;i<=M;i++){
            scanf("%d%d%d",&u,&v,&w);
            add(v,u,w); add(u,v,w);
        }
        SPFA();
        for(int i=1;i<=cnt;i++){
            if(dis[To[i]]>dis[From[i]]){
                G[To[i]].push_back(From[i]);
                ind[From[i]]++;
            }
        }
        queue<int>q; dp[1]=1;
        for(int i=1;i<=N;i++) if(ind[i]==0) q.push(i),vis[i]=1;
        while(!q.empty()){
            int u=q.front(); q.pop();
            for(int i=0;i<G[u].size();i++){
                int v=G[u][i];  if(vis[v]) continue;
                dp[v]+=dp[u]; ind[v]--;
                if(ind[v]==0&&vis[v]==0){ q.push(v); vis[v]=1;}
            }
        }
        printf("%d
",dp[2]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hua-dong/p/9890157.html