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

第一道最短路

Posted by su123456 at 2014-08-06 15:40:00 on Problem 1062
RT
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#define MAXP(x,y)  (((x)>(y))?(x):(y))
#define MINP(x,y)  (((x)<(y))?(x):(y))
const int MAX=0xfffffff;
using namespace std;
const int mx=110;
int map[mx][mx],level[mx],dist[mx];
bool vis[mx];
int n;
int dijkstra( )
{
    for(int i=0;i<=n;i++)
        dist[i]=map[0][i];
    vis[0]=1;
    for(int i=1;i<n;i++)
    {
        int mint=MAX;
        int k=0;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dist[j]<mint)
            {
                mint=dist[j];
                k=j;
            }
        }
        vis[k]=1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dist[j]>dist[k]+map[k][j])
                dist[j]=dist[k]+map[k][j];
        }
    }
    return dist[1];
}
int main( )
{
    //freopen("1.txt","r",stdin);
    int m;
    while(cin>>m>>n)
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                if(i==j)  map[i][j]=0;
                else  map[i][j]=MAX;
        memset(level,0,sizeof(level));
        memset(vis,0,sizeof(vis));
        int n1;
        for(int i=1;i<=n;i++)
        {
            cin>>map[0][i]>>level[i]>>n1;
            for(int j=1;j<=n1;j++)
            {
                int t;
                cin>>t;
                cin>>map[t][i];
            }
        }
        int minp=MAX;
        for(int i=1;i<=n;i++)
        {
            int lv=level[i];
            for(int j=1;j<=n;j++)
            {
                if(level[j]>lv||lv-level[j]>m)
                    vis[j]=1;
                else
                    vis[j]=0;
            }
            int mint=dijkstra( );
            if(minp>mint)
                minp=mint;
        }
        cout<<minp<<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