标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7330 Accepted Submission(s): 2687
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int N = 1005; const int INF = 99999999; int graph[N][N]; int n,m; bool vis[N]; int low[N]; int dp[N]; void dijkstra(int n,int pos) { memset(vis,0,sizeof(vis)); vis[pos]=1; for(int i=1; i<=n; i++) low[i]=graph[pos][i]; low[pos] = 0; for(int i=1; i<n; i++) { int Min=INF; for(int j=1; j<=n; j++) if(vis[j]==0&&Min>low[j]) { pos=j; Min=low[j]; } vis[pos]=1; for(int j=1; j<=n; j++) if(vis[j]==0&&low[j]>graph[pos][j]+low[pos]) { low[j]=graph[pos][j]+low[pos]; } } } int dfs(int s,int n){ if(dp[s]>0) return dp[s]; int ans = 0; for(int i=1;i<=n;i++){ if(graph[s][i]<INF&&low[s]>low[i]&&i!=s){ ans+=dfs(i,n); } } dp[s] = ans; return dp[s]; } int main() { while(scanf("%d",&n)!=EOF,n) { scanf("%d",&m); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) graph[i][j]=INF; } for(int i=1; i<=m; i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); graph[a][b]=graph[b][a] = c; } dijkstra(n,2); memset(dp,0,sizeof(dp)); dp[2]=1; printf("%d\n",dfs(1,n)); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5475524.html