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

y放代码在这里

Posted by 201030720525 at 2011-04-24 14:37:10 on Problem 2387
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1005
#define min(a,b) a<b?a:b
const int INF=99999999;
using namespace std;

int n,m;
int dist[maxn];
int maz[maxn][maxn];
bool visit[maxn];
void dijkstra(int s)
{
    int i,j;
    for(i=1;i<=n;i++)
        dist[i]=INF,visit[i]=0;
    dist[s]=0;
    for(i=1;i<=n;i++)
    {
        int min=INF,u=0;
        for(j=1;j<=n;j++)
            if(!visit[j]&&min>dist[j])
            {
                min=dist[j];
                u=j;
            }
        if(u==0)
            break;
        visit[u]=1;
        for(j=1;j<=n;j++)
            if(!visit[j]&&dist[j]>dist[u]+maz[u][j])
                dist[j]=dist[u]+maz[u][j];
    }
     return;
}
int main()
{
    int i,j,u,v,w;
    scanf("%d%d",&m,&n);
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            maz[i][j]=INF;
    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        maz[u][v]=maz[v][u]=min(w,maz[v][u]);
    }
    dijkstra(n);
    printf("%d\n",dist[1]);
    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