UVA10917 Walk Through the Forest

Posted zytwan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10917 Walk Through the Forest相关的知识,希望对你有一定的参考价值。

最短路,然后呀,扫一遍路径个数。。。感觉对dp还不是很熟

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxn=10005;
  4 const int maxm=500005;
  5 const int INF=1000000000;
  6 
  7 inline int read()
  8 {
  9     int x=0,k=1;char c=getchar();
 10     while(c<0||c>9) {if(c==-) k=-1;c=getchar();}
 11     while(c>=0&&c<=9) x=(x<<3)+(x<<1)+(c^48),c=getchar();
 12     return x*k;
 13 }
 14 
 15 struct Edge{
 16     int u,v,w,next;
 17 }e[maxm];
 18 
 19 int head[maxn],cnt,n,m,vis[maxn],dis[maxn],f[maxm];
 20 
 21 struct node{
 22     int w,now;
 23     inline bool operator< (const node &x) const
 24     {
 25         return w>x.w;
 26     }
 27 };
 28 
 29 priority_queue<node>q;
 30 
 31 inline void add(int u,int v,int w)
 32 {
 33     e[++cnt].u=u;
 34     e[cnt].v=v;
 35     e[cnt].w=w;
 36     e[cnt].next=head[u];
 37     head[u]=cnt;
 38 }
 39 
 40 void dijikstra()
 41 {
 42     for(int i=1;i<=n;++i) {dis[i]=INF;vis[i]=0;}
 43     dis[2]=0;
 44     q.push((node){0,2});
 45     while(!q.empty())
 46     {
 47         node x=q.top();
 48         q.pop();
 49         int u=x.now;
 50         if(vis[u]) continue;
 51         vis[u]=1;
 52         for(int i=head[u];i;i=e[i].next)
 53         {
 54             int v=e[i].v;
 55             if(dis[v]>dis[u]+e[i].w)
 56             {
 57                 dis[v]=dis[u]+e[i].w;
 58                 q.push((node){dis[v],v});
 59             }
 60         }
 61     }
 62 }
 63 
 64 void init()
 65 {
 66     memset(head,0,sizeof(head));
 67     cnt=0;
 68 }
 69 
 70 int search(int k)                     
 71 {
 72     if(vis[k]==1) return f[k];
 73     vis[k]=1;
 74     if(k==2) {vis[k]=1;return f[k]=1;}
 75     for(int i=head[k];i;i=e[i].next)
 76     {
 77         int v=e[i].v;
 78         if(dis[v]<dis[k])
 79         {
 80             f[k]+=search(v);
 81         }
 82     }
 83     return f[k];
 84 }
 85 
 86 int main()
 87 {
 88     while(scanf("%d",&n)==1&&n)
 89     {
 90         m=read();
 91         init();
 92         for(int i=1,x,y,z;i<=m;++i)
 93         {
 94             x=read(),y=read(),z=read();
 95             add(x,y,z);
 96             add(y,x,z);
 97         }
 98         dijikstra();
 99         memset(vis,0,sizeof(vis));
100         memset(f,0,sizeof(f));
101         printf("%d
",search(1));
102     }
103 }

 

以上是关于UVA10917 Walk Through the Forest的主要内容,如果未能解决你的问题,请参考以下文章

UVA10917 Walk Through the Forest

Uva10917 Walk Through the Forest

UVA10917 Walk Through the Forest

UVA10917 Walk Through the Forest

G - Walk Through the Forest (UVA - 10917)

UVA 10917 Walk Through the Forest(Dijkstra+DAG动态规划)