Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

快过来看一下呐,Dijkstra算法,依次比较各点,求最小值,总是Runtime Error!

Posted by Jaster_scz at 2016-03-12 14:15:42 on Problem 1125
#include <iostream>
using namespace std;

#define MAX 1<<29
int n;
bool vis[101];
int dis[101][101];
int d[101];    //表示到各点的最短距离

int dijkstra(int start){
    int i,j,k,dist;
    int result;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=n;i++){
        if(i == start)
            d[i] = 0;
        else
            d[i] = MAX;
    }
    for(i=1;i<=n;i++){
        dist = MAX;
        k = 0;
        for(j=1;j<=n;j++){
            if(!vis[j]&&d[j]<=dist){
                k = j;
                dist = d[j];
            }
        }
        vis[k] = 1;
        for(j=1;j<=n;j++){
            if(d[k]+dis[k][j]<d[j])
                d[j] = d[k] + dis[k][j];
        }
    }
    result = 0;
    for(i=1;i<=n;i++){
        if(d[i] > result)
            result = d[i];
    }
    return result;
}

int main(){
    int i,j,k,friends;
    int temp,result,index;
    while(cin>>n,n){
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
                dis[i][j] = MAX;
        for(i=1;i<=n;i++){
            cin>>friends;
            for(j=0;j<friends;j++){
                cin>>k>>dis[i][k];
            }
        }
        result = MAX;
        index = 0;
        for(i=1;i<=n;i++){
            temp = dijkstra(i);
            if(temp<result){
                index = i;
                result = temp;
            }
        }
        if(index == 0)
            cout<<"disjoint"<<endl;
        else
            cout<<index<<" "<<result<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator