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();
    	}
    }


  • 相关阅读:
    如何解决Ubuntu下的的“system program problem detected”
    GDM, KDM, LightDM, SDDM显示管理器的区别和安装配置
    在Linux下安装使用vokoscreen录制网站上的视频与声音-Ubuntu-mate-20.04桌面环境测试录屏推荐
    Ubuntu-mate-20.04-desktop安装总结
    云上渗透-基于阿里云环境渗透
    fastadmin前台getshell漏洞
    gitlab CI/CD Custom SAST
    前端神器RRWEB
    【转】六种减小Docker镜像大小的方法
    splunk 多个数据关联查询
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7107160.html
Copyright © 2011-2022 走看看