UVa10917 Walk Through the Forest


题目描述 传送门


根据题目意思建立DAG,再统计路径条数。


代码

#include
#include
#include
#include
#include
using namespace std;
const int maxn=1005,maxm=(maxn-1)*(maxn-2);
const int INF=1e9;
int a[maxn],d[maxn],h[maxn],cnt,n,m;
bool vis[maxn];
struct Edge{
    int t,d,nxt;
    Edge(int a=0,int b=0,int c=0):t(a),d(b),nxt(c){}
}edges[maxm*2];
struct heap{
    int d,v;
    heap(int a=0,int b=0):d(a),v(b){}
    bool operator<(const heap&a)const{
        return d>a.d;
    }
};
void addedge(int from,int to,int dist){
    edges[++cnt]=Edge(to,dist,h[from]);
    h[from]=cnt;
    edges[++cnt]=Edge(from,dist,h[to]);
    h[to]=cnt;
}
int dfs(int u){
    if(a[u]>-1) return a[u];
    a[u]=0;
    for(int i=h[u];i;i=edges[i].nxt){
        int v=edges[i].t;
        if(d[u]return a[u];
}
int main(){
    while(scanf("%d",&n)==1&&n){
        scanf("%d",&m);
        cnt=0;
        memset(h,0,sizeof(h));
        for(int i=0;iint a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
        }
        priority_queue q;
        q.push(heap(0,2));
        for(int i=1;i<=n;i++) d[i]=INF;
        d[2]=0;
        memset(vis,0,sizeof(vis));
        while(!q.empty()){
            heap x=q.top();q.pop();
            int u=x.v;
            if(vis[u]) continue;
            vis[u]=1;
            for(int i=h[u];i;i=edges[i].nxt){
                int v=edges[i].t;
                if(d[u]+edges[i].dmemset(a,-1,sizeof(a));
        memset(vis,0,sizeof(vis));
        a[1]=1;
        printf("%d\n",dfs(2));
    }
    return 0;
}

你可能感兴趣的:(路径统计,UVa,最短路)