[問題] core dumped

作者: withoutwords (得意忘言)   2014-09-01 23:53:52
我想跑一個把網路轉成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]];
}
作者: demisoda (demisoda)   2014-09-02 00:08:00
編譯 -g 並用 gdb 試試看囉
作者: uranusjr (←這人是超級笨蛋)   2014-09-02 11:24:00
你的 dist 是 int[] 怎麼可以把 INF 放進去其實編譯警告就有提示到重點, 你想想它為什麼這樣寫
作者: kerwinhui (kezza)   2014-09-02 14:04:00
總得先給個example file吧?沒argv[1]的話自然會當掉如果argv[1]是一個空檔,是不會當掉的

Links booklink

Contact Us: admin [ a t ] ucptt.com