zoukankan      html  css  js  c++  java
  • Seaside HDU 3665 【Dijkstra】

    Problem Description
    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


    //Dijkstra

    #include <stdio.h>
    #include <string.h>
    #define INF 0x3f3f3f3f
    int N;
    int n;
    int low[20];
    bool vis[20];
    int map[20][20];
    void Dijkstra()
    {
    	int pos=0;
    	int i,j;
    	memset(vis,0,sizeof(vis));
    	memset(low,INF,sizeof(low));
    	for(i=0;i<n;++i)
    	{
    		low[i]=map[0][i];
    	}
    	vis[0]=1;
    	low[0]=0;
    	for(i=0;i<n-1;++i)
    	{
    		int min=INF;
    		for(j=0;j<n;++j)
    		{
    			if(!vis[j]&&min>low[j])
    			{
    				min=low[j];
    				pos=j;
    			}
    		}
    		vis[pos]=1;
    		for(j=0;j<n;++j)
    		{
    			if(!vis[j]&&low[j]>map[pos][j]+low[pos])
    				low[j]=map[pos][j]+low[pos];
    		}
    	}
    	printf("%d
    ",low[n-1]);
    }
    int main()
    {
    	int i, j;
    	int M, flag;
    	int u,v,w;
    	while(~scanf("%d", &N))
    	{
    		n=N+1;
    		for(i=0;i<=N;++i)
    			for(j=0;j<=i;++j)
    				map[i][j]=map[j][i]=INF;
    		//memset(map,INF,sizeof(map));
    		for(int i = 0; i < N; ++i)
    		{
    			scanf("%d%d", &M, &flag);
    			if(flag) map[i][n-1]=map[n-1][i]=0;	
    			while(M--)
    			{
    				scanf("%d%d",&v,&w);
    				if(map[i][v]>w)
    					map[i][v]=map[v][i]=w;							
    			}
    		}
    		Dijkstra();
    	}
    }


  • 相关阅读:
    03_线性表
    02_算法与数据结构
    01_python中内置类型的时间复杂度
    00_常见的时间复杂度
    03_docker导出和导入镜像
    09_创建mysql数据库的用户
    14_linux添加主机列表
    13_linux修改主机名
    12_centos7安装好后的网络设置
    00_使用pdb调试python代码
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7107160.html
Copyright © 2011-2022 走看看