50,713
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int dp[];
public static int dis[];
public static int vis[];
public static int map[][];
public static int vsum,road;
public static int INF=0x7fffffff;
public static int N=1010;
public static void dj(int start)//地杰斯特拉
{
int i,j,k=0;
Arrays.fill(vis, 0);
for(i=1;i<=vsum;i++)
dis[i]=map[start][i];
dis[start]=0;
vis[start]=1;
for(i=1;i<=vsum;i++)
{
int temp=INF;
for(j=1;j<=vsum;j++)
{
if(vis[j]!=1&&dis[j]<temp)
{temp=dis[k=j];break;}
}
if(temp==INF) break;
vis[k]=1;
for(j=1;j<=vsum;j++)
{
if(vis[j]!=1&&dis[j]>dis[k]+map[k][j])
dis[j]=dis[k]+map[k][j];
}
}
}
public static int dfs(int v)
{
if(v==2) return 1;
if(dp[v]!=-1) return dp[v];
int sum=0;
for(int i=1;i<=vsum;i++)
{
if(map[v][i]!=INF&&dis[v]>dis[i])
sum+=dfs(i);
}
dp[v]=sum;
return dp[v];
}
public static void main(String args[]) {
Scanner in=new Scanner(new BufferedInputStream(System.in));
while(in.hasNext())
{
vsum=in.nextInt();
if(vsum==0)break;
road=in.nextInt();
dis=new int[vsum+1];
dp=new int[vsum+1];
vis=new int[vsum+1];
map=new int[vsum+1][vsum+1];
int i,j;
for(i=1;i<=vsum;i++)
for(j=1;j<=vsum;j++)
map[i][j]=(i==j?0:INF);
Arrays.fill(dp,-1);
int u,v,w;
for(i=1;i<=road;i++)
{
u=in.nextInt();v=in.nextInt();w=in.nextInt();
map[u][v]=w;map[v][u]=w;
}
dj(2);
System.out.println(dfs(1));
}
}
}