我想跑一个把网络转成shortest path tree的程式,但是一直无法成功执行
在dev C++编译会出现
In function `void spfa()':
[Warning] converting to `int' from `double'
执行时会出现“已经停止运作”的字样
改到ubuntu上以g++编译,则在执行时会出现core dumped
试了很久还是无法成功,希望能有好心人不吝告知问题出在哪里,感激不尽!
以下是程式码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define N_NODES 10000
#define INF 1e9
using namespace std;
struct Edge{
int src, tgt;
int weight;
Edge(){}
Edge(int tsrc, int ttgt, int tweight){
src = tsrc;
tgt = ttgt;
weight = tweight;
}
bool operator < (const Edge &e)const{
if(src != e.src) return src < e.src;
return tgt < e.tgt;
}
};
int n_nodes, n_edges, n_K, r;
int bt, sumBt, ct;
double w;
int setK[N_NODES];
vector<Edge> edge[N_NODES];
vector<Edge> treeEdge;
queue<int> que;
int par[N_NODES][2];
int degree[N_NODES];
int dist[N_NODES];
bool inque[N_NODES];
bool intree[N_NODES];
int n_intree;
void spfa();
void calDegree();
void calTreeWeight();
void output();
int main(int argc, char* argv[])
{
int i, j;
int u, v, weight;
freopen(argv[1], "r", stdin);
freopen(strcat(argv[1], "_spTree_output_version.out"), "w+", stdout);
while(~scanf("%d%d%d%d%lf", &n_nodes, &n_edges, &n_K, &r, &w)){
// read in input
for(i=0; i<n_K; ++i)
scanf("%d", &setK[i]);
for(i=0; i<n_nodes; ++i)
edge[i].clear();
for(i=0; i<n_edges; ++i){
scanf("%d%d%d", &u, &v, &weight);
edge[u].push_back(Edge(u, v, weight));
edge[v].push_back(Edge(v, u, weight));
}
spfa();
memset(intree, 0, sizeof(intree));
intree[r] = true;
n_intree = 1;
treeEdge.clear();
for(i=0; i<n_K; ++i){
for(j=setK[i]; j!=r && !intree[j]; j=par[j][0]){
intree[j] = true;
++n_intree;
treeEdge.push_back(Edge(par[j][0], j, par[j][1]));
}
}
sort(treeEdge.begin(), treeEdge.end());
calDegree();
calTreeWeight();
output();
//printf("%f %f\n", (double)bt*w+(double)ct,
(bt==0)?-1:(double)sumBt/(double)bt);
}
return 0;
}
void spfa(){
int i, j;
int cur, next;
memset(inque, 0, sizeof(inque));
while(!que.empty()) que.pop();
for(i=0; i<n_nodes; ++i)
dist[i] = INF;
dist[r] = 0;
par[r][0] = r;
que.push(r);
inque[r] = true;
while(!que.empty()){
cur = que.front(); que.pop();
inque[cur] = false;
for(j=0; j<(int)edge[cur].size(); ++j){
next = edge[cur][j].tgt;
if(dist[cur] + edge[cur][j].weight < dist[next]){
dist[next] = dist[cur] + edge[cur][j].weight;
par[next][0] = cur;
par[next][1] = edge[cur][j].weight;
if(!inque[next]){
que.push(next);
inque[next] = true;
}
}
}
}
}
void calDegree(){
int i;
memset(degree, 0, sizeof(degree));
for(i=0; i<n_nodes; ++i){
if(intree[i])
++degree[par[i][0]];
}