zoukankan      html  css  js  c++  java
  • hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142

    这道题是spfa求最短路,然后dfs()求路径数。

      1 #include <cstdio>
      2 #include <queue>
      3 #include <cstring>
      4 #include <algorithm>
      5 #define maxn 1001
      6 using namespace std;
      7 const int inf=1<<30;
      8 
      9 int g[maxn][maxn];
     10 int dis[maxn];
     11 bool vis[maxn],visi[maxn];
     12 int n,m,a,b,d;
     13 int di;
     14 int cnt[maxn];
     15 int num[maxn];
     16 
     17 bool spfa()
     18 {
     19     queue<int>q;
     20     memset(vis,false,sizeof(vis));
     21     memset(cnt,0,sizeof(cnt));
     22     for(int i=1; i<=n; i++) dis[i]=inf;
     23     dis[2]=0;
     24     vis[2]=true;
     25     q.push(2);
     26     while(!q.empty())
     27     {
     28         int u=q.front();
     29         q.pop();
     30         vis[u]=false;
     31         for(int i=1; i<=n; i++)
     32         {
     33             if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf)
     34             {
     35                 dis[i]=dis[u]+g[u][i];
     36                 if((++cnt[i])>n) return false;
     37                 if(!vis[i])
     38                 {
     39                     q.push(i);
     40                     vis[i]=true;
     41                 }
     42             }
     43         }
     44     }
     45     return true;
     46 }
     47 
     48 void dfs(int src)
     49 {
     50     if(src==2)
     51     {
     52         num[2]=1;
     53         return ;
     54     }
     55     int sum=0;
     56     for(int i=1; i<=n; i++)
     57     {
     58         if(g[src][i]!=inf&&dis[src]>dis[i])
     59         {
     60             if(num[i]>=0)
     61             {
     62                 sum+=num[i];
     63             }
     64             else
     65             {
     66                 dfs(i);
     67                 sum+=num[i];
     68             }
     69         }
     70     }
     71     num[src]=sum;
     72 }
     73 int main()
     74 {
     75     while(scanf("%d",&n)!=EOF)
     76     {
     77         if(n==0) break;
     78         scanf("%d",&m);
     79         for(int i=1; i<=n; i++)
     80         {
     81             for(int j=1; j<=n; j++)
     82             {
     83                 if(i==j) g[i][j]=0;
     84                 else g[i][j]=inf;
     85             }
     86         }
     87         for(int i=0; i<m; i++)
     88         {
     89             scanf("%d%d%d",&a,&b,&d);
     90             g[a][b]=g[b][a]=min(g[a][b],d);
     91         }
     92         spfa();
     93         //printf("%d
    ",dis[1]);
     94         for(int i=1; i<=n; i++)
     95         {
     96             num[i]=-1;
     97         }
     98         dfs(1);
     99         printf("%d
    ",num[1]);
    100     }
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    【codeforces 791D】 Bear and Tree Jumps
    【codeforces 791C】Bear and Different Names
    【codeforces 791B】Bear and Friendship Condition
    【codeforces 791A】Bear and Big Brother
    【t017】YL杯超级篮球赛
    Java Web整合开发(80) -- EJB & WebService
    搜索与排序
    T2821 天使之城 codevs
    T1155 金明的预算方案 codevs
    后缀表达式
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3687307.html
Copyright © 2011-2022 走看看