hdu3665(最短路)

来源:互联网 发布:社交网络未来趋势 编辑:程序博客网 时间:2024/06/01 21:27

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 longusing 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 longusing 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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苏宁退货不成功怎么办 任性付冻结了怎么办 任性贷风险提示怎么办 保修发票没了怎么办 家电发票丢了怎么办 天猫苏宁发票丢了怎么办 本子密码忘记了怎么办 直属领导抢客户怎么办 老板油烟机坏了怎么办 闲鱼上买洗衣机自提怎么办 水龙头水流太小怎么办 水龙头压力太小怎么办 浴室水龙头水小怎么办 冰箱出水口堵了怎么办 水龙头出水太小怎么办 买东西让骗了怎么办 医疗保险中断5年怎么办 医保断交5年怎么办 医保社保断了怎么办 医保没交够25年怎么办 医保卡外借 保险怎么办 安装系统时断电怎么办 电视不带wifi怎么办 苹果手机呼叫失败怎么办 苹果8呼叫失败怎么办 微信被朋友拉黑怎么办 手机骚扰电话太多怎么办 骚扰电话太多了怎么办 总是接到骚扰电话怎么办 老有骚扰电话怎么办 一直被电话轰炸怎么办 有线电视突然出来雪花怎么办 电视出雪花了怎么办 长虹电视有雪花怎么办 网络电视雪花了怎么办 夏普电视雪花屏怎么办 有线电视显示无信号怎么办 门铃对讲机坏了怎么办 蝴蝶兰叶子晒伤怎么办 杜鹃花浇水多了怎么办 杜鹃花施肥多了怎么办