水题1A。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1000+5;
const int inf=100000000;
struct HeapNode{
int d,u;
bool operator < (const HeapNode &rhs) const {
return d>rhs.d;
}
};
struct edge{
int from,to,dist;
};
struct Dijkstra{
int n,m;
vector<edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
int p[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist){
edges.push_back((edge){from,to,dist});
m=(int)edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s){
priority_queue<HeapNode> pq;
for(int i=0;i<n;i++) d[i]=inf;
d[s]=0;
memset(done,0,sizeof(done));
pq.push((HeapNode){0,s});
while(!pq.empty()){
HeapNode x=pq.top();pq.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();i++){
edge &e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
pq.push((HeapNode){d[e.to],e.to});
}
}
}
}
};
Dijkstra dij;
int n,m;
int f[maxn];
int dfs(int u){
if(u==1) return f[u];
if(f[u]!=-1) return f[u];
f[u]=0;
for(int i=0;i<dij.G[u].size();i++){
edge e=dij.edges[dij.G[u][i]];
if(dij.d[e.to]<dij.d[e.from]) f[u]+=dfs(e.to);
}
return f[u];
}
int main()
{
while(scanf("%d",&n)&&n)
{
dij.init(n);
scanf("%d",&m);
for(int i=0;i<m;i++){
int u,v,dist;
scanf("%d%d%d",&u,&v,&dist);
u--;v--;
dij.AddEdge(u,v,dist);
dij.AddEdge(v,u,dist);
}
dij.dijkstra(1);
memset(f,-1,sizeof(f));
f[1]=1;
int ans=dfs(0);
printf("%d\n",ans);
}
return 0;
}