hdu1142-A Walk Through the Forest-最短路+dfs

原创 2012年03月21日 20:45:35
下面是spfa+dfs
//5601720	2012-03-21 20:40:48	Accepted	1142	31MS	624K	1382 B	C++	zjwzcnjsy
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define inf 0x3fffffff
#define M 1001

struct son{
    int v,w;
    son(int tv,int tw):v(tv),w(tw){}
};
vector<son>g[M];
int n,m;
int dist[M],inq[M];
__int64 ans[M];;

void init(){
    for(int i=0;i<=n;++i)g[i].clear(),ans[i]=-1;
}
void spfa(int u){
    int i,v,w;
    for(i=1;i<=n;++i)dist[i]=inf,inq[i]=0;
    dist[u]=0;
    queue<int>Q;
    Q.push(u);inq[u]=1;
    while(!Q.empty()){
        u=Q.front();Q.pop();inq[u]=0;
        for(i=0;i<g[u].size();++i){
            v=g[u][i].v;w=g[u][i].w;
            if(dist[v]>dist[u]+w){
                dist[v]=dist[u]+w;
                if(!inq[v]){Q.push(v);inq[v]=1;}
            }
        }
    }
}
__int64 dfs(int i){/*dfs也要优化,不然也会超时*/ 
    if(ans[i]!=-1)return ans[i];
    if(i==2)return 1;
    ans[i]=0;
    for(int j=0;j<g[i].size();++j)
    {
        if(g[i][j].v!=i && dist[g[i][j].v]<dist[i]){
            ans[i]+=dfs(g[i][j].v);
        }
    }
    return ans[i];
}
int main()
{
    int a,b,d,i;
    while(scanf("%d",&n)!=EOF&&n){
        scanf("%d",&m);
        init();
        for(i=0;i<m;++i){
            scanf("%d%d%d",&a,&b,&d);
            g[a].push_back(son(b,d));
            g[b].push_back(son(a,d));
        }
        spfa(2);
        dfs(1);
        printf("%I64d\n",ans[1]);
    }
    return 0;
}

下面是dijkstra+dfs

//5601763	2012-03-21 20:44:11	Accepted	1142	93MS	4156K	1317 B	C++	zjwzcnjsy
#include <stdio.h>
#define inf 0x3fffffff
#define M 1001

int dist[M],map[M][M],n,m;
__int64 ans[M];
void init(){
    for(int i=1;i<=n;++i){
        ans[i]=-1;
        for(int j=1;j<=n;++j)map[i][j]=(i==j?0:inf);
    }
}
void dijkstra(int u){
    int v[M],i,j,min,w;
    for(i=1;i<=n;++i)dist[i]=map[u][i],v[i]=0;
    dist[u]=0;v[u]=1;
    for(i=0;i<n;i++){
        min=inf;
        for(j=1;j<=n;++j){
            if(!v[j] && dist[j]<min){min=dist[j];w=j;}
        }
        v[w]=1;
        for(j=1;j<=n;++j){
            if(!v[j] && dist[j]>dist[w]+map[w][j]){dist[j]=dist[w]+map[w][j];}
        }
    }
}
__int64 dfs(int i){/*dfs也要优化,不然也会超时*/ 
    if(ans[i]!=-1)return ans[i];
    if(i==2)return 1;
    ans[i]=0;
    for(int j=1;j<=n;++j)
    {
        if(i!=j && dist[j]<dist[i] && map[i][j]<inf){
            ans[i]+=dfs(j);
        }
    }
    return ans[i];
}
int main()
{
    int a,b,d,i;
    while(scanf("%d",&n)!=EOF&&n){
        scanf("%d",&m);
        init();
        for(i=0;i<m;++i){
            scanf("%d%d%d",&a,&b,&d);
            if(map[a][b]>d)map[a][b]=map[b][a]=d;
        }
        dijkstra(2);
        dfs(1);
        printf("%I64d\n",ans[1]);
    }
    return 0;
}


相关文章推荐

hdu1142A Walk Through the Forest (最短路+BFS+DFS )

Problem Description Jimmy experiences a lot of stress at work these days, especially since his acci...

【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest

/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------...

HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)

Problem Description Jimmy experiences a lot of stress at work these days, especially since his ac...

hdu-1142-A Walk Through the Forest-最短路+记忆化搜索

A Walk Through the Forest Problem Description Jimmy experiences a lot of stress at work thes...

HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja...

HDU 1142 A Walk Through the Forest 最短路+记忆搜索

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja...

hdu1142A Walk Through the Forest(spfa+dfs+标记,题目意思要注意)

Problem Description Jimmy experiences a lot of stress at work these days, especially since his acci...

hdu 1142 A Walk Through the Forest(最短路径+dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1142 大致题意:若a,b之间有边,走ab边的条件是b到目的地的最短路径小于a到目的地的最短路径。按这个...

HDU 1142 A Walk Through the Forest (搜索-DFS)

题目意思:寻找一共有多少条符合题意的路。能够从点A走到点B的要求是:点A到终点的最短路 > 点B到终点的最短路。先一遍迪杰斯特拉,然后一边记忆化dfs搜索就可以了。...

HDU1142:A Walk Through the Forest(Dijkstra+dfs)

Problem Description Jimmy experiences a lot of stress at work these days, especially since his acci...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu1142-A Walk Through the Forest-最短路+dfs
举报原因:
原因补充:

(最多只允许输入30个字)