hdu3665(最短路)

Seaside
XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.

Input
There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that the distance between the i-th town and the SMi town is LMi.

Output
Each case takes one line, print the shortest length that XiaoY reach seaside.

Sample Input
5
1 0
1 1
2 0
2 3
3 1
1 1
4 100
0 1
0 1

Sample Output
2

题意大概:给出N个城市,每个城市分靠海和不靠海,给出几条路,求0号城市到距离他最近的城市的距离。
最短路分为
1、单源最短路:可用dijkstra时间复杂度小;
2、全局最短路:用floyd-warshall算法,求任两点之间的最短距离;
3、两点最短路:用广搜或深搜解决;

此题典型单源最短路,因为城市数量少,用dijkstra算法和Floyd算法都可。
运用Floyd-warshall方便理解,即运用三个for循环嵌套,求每两个点之间的最短距离。
状态转移方程:map[i][j] = min( map[i][j] , map[i][k]+map[k][j]);
Floyd-warshall代码

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int main()
{
    int N;
    while(cin>>N)
    {
        int n,a,b;
        int d[15],v[15],w[15][15],p[15];
        for(int i=0; i<N; i++)
            for(int j=0; j<N; j++)
                w[i][j]=INF;
        for(int i=0; i<N; i++)
        {
            cin>>n>>p[i];
            for(int j=0; j<n; j++)
            {
                cin>>a>>b;
                w[i][a] = w[a][i] = b;
            }
        }
        for(int k=0; k<N; k++)
            for(int i=0; i<N; i++)
                for(int j=0; j<N; j++)
                    if(w[i][k]+w[k][j]<w[i][j])
                        w[i][j]=w[i][k]+w[k][j];
        int re = INF;
        for(int i=0; i<N; i++)
            if(w[0][i]<re&&p[i])
                re = w[0][i];
        cout<<re<<endl;
    }
}

dijkstra算法(单源)【设:从n到其他点】
1、d[j]表示点n到其他每个点的距离。
初始化集合S包含所有点,集合M空。
初始化 所有没有权值的map[i][j]为+∞,d[j]为+∞,d[n]=0。
2、首先求出集合S中距离n最短的一个点,k记录下来,把他放在集合M中,求出M集合中n通过k到其他点和n直接到其他店的最短距离方程。
状态方程: d[j]=min( d[j] , d[k]+map[k][j] );
3、重复步骤(2)直到集合S为空。这时点n到其余每个点都为最短距离!
主算法如下:

void dij()
{
    int k,p[15]={0};
    memset(d,INF,sizeof(d));
    d[0]=0;
    for(int i=0; i<N; i++)
        {
            int min1=INF;
            for(int j=0; j<N; j++)
                if(!p[j]&&d[j]<min1)
                {
                    min1 = d[j];
                    k = j;
                }
            p[k] = 1;
            for(int j=0; j<N; j++)
                if(!p[j]&&d[j]>d[k]+map[k][j])
                    d[j]=d[k]+map[k][j];
        }
}

dijkstraAC代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int N;
int d[15];
int p[15];
int map[15][15];
int v[15];
void dij();
int main()
{
    while(cin>>N)
    {
        int n,a,b;
        for(int i=0; i<N; i++)
            for(int j=0; j<N; j++)
                map[i][j]=INF;

        for(int i=0; i<N; i++)  //输入函数
        {
            cin>>n>>v[i];
            for(int j=0; j<n; j++)
            {
                cin>>a>>b;
                map[i][a] = map[a][i] = b;
            }
        }
        dij();          //中心函数

        int re = INF;
        for(int i=0; i<N; i++)       //输出函数
        {
            if(d[i]<re&&v[i])
                re = d[i];
        }
        cout<<re<<endl;
    }
}
void dij()
{
    int k,p[15]={0};
    memset(d,INF,sizeof(d));
    d[0]=0;
    for(int i=0; i<N; i++)
        {
            int min1=INF;
            for(int j=0; j<N; j++)
                if(!p[j]&&d[j]<min1)
                {
                    min1 = d[j];
                    k = j;
                }
            p[k] = 1;
            for(int j=0; j<N; j++)
                if(!p[j]&&d[j]>d[k]+map[k][j])
                    d[j]=d[k]+map[k][j];
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值