• (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。


    floyd解法

    今天初看dijkstra,先拿这两题练手,其他变形题还是不是很懂。

    模版题,纯练打字。。。

    HDU 1874:


    #include <cstdio>
    
    #define MAXN 200
    #define INF 0xfffff 
    
    int n;
    int Edge[MAXN][MAXN];
    int s[MAXN];
    int dist[MAXN];
    int path[MAXN];
    
    void Dijkstra(int v0) {
    	int i, j, k;
    	for (i = 0; i < n; i++) {
    		dist[i] = Edge[v0][i];
    		s[i] = 0;
    		if (i != v0 && INF > dist[i])
    			path[i] = v0;
    		else
    			path[i] = -1;
    	}
    	s[v0] = 1;
    	dist[v0] = 0;
    	for (i = 0; i < n - 1; i++) {
    		int min = INF, u = v0;
    		for (j = 0; j < n; j++)
    			if (!s[j] && dist[j] < min){
    				u = j;
    				min = dist[j];
    			}//if
    		s[u] = 1;
    		for (k = 0; k < n; k++) {
    			if (!s[k] && Edge[u][k] < INF && dist[u] + Edge[u] [k] < dist[k]) {
    				dist[k] = dist[u] + Edge[u][k];
    				path[k] = u;
    			}//if
    		}//for
    	}//for
    }
    
    int main() {
    	int m;
    	int i, j;
    	while (scanf("%d%d", &n, &m) != EOF) {
    		for (i = 0; i < n; i++)
    			for (j = 0; j < n; j++)
    				if (i == j)
    					Edge[i][j] = 0;
    				else
    					Edge[i][j] = INF;
    		int x, y, z;
    		for (i = 0; i < m; i++) {
    			scanf("%d%d%d", &x, &y, &z);
    			if (z < Edge[x][y])
    				Edge[y][x] = Edge[x][y] = z;
    		}
    		scanf("%d%d", &x, &y);
    		Dijkstra(x);
    		if (dist[y] < INF)
    			printf("%d
    ", dist[y]);
    		else
    			printf("-1
    ");
    	}//while
    	return 0;
    }


    HDU 2544:

    #include <cstdio>
    
    #define MAXN 200
    #define INF 0xfffff 
    
    int n;
    int Edge[MAXN][MAXN];
    int s[MAXN];
    int dist[MAXN];
    int path[MAXN];
    
    void Dijkstra(int v0) {
    	int i, j, k;
    	for (i = 0; i < n; i++) {
    		dist[i] = Edge[v0][i];
    		s[i] = 0;
    		if (i != v0 && INF > dist[i])
    			path[i] = v0;
    		else
    			path[i] = -1;
    	}
    	s[v0] = 1;
    	dist[v0] = 0;
    	for (i = 0; i < n - 1; i++) {
    		int min = INF, u = v0;
    		for (j = 0; j < n; j++)
    			if (!s[j] && dist[j] < min){
    				u = j;
    				min = dist[j];
    			}//if
    		s[u] = 1;
    		for (k = 0; k < n; k++) {
    			if (!s[k] && Edge[u][k] < INF && dist[u] + Edge[u] [k] < dist[k]) {
    				dist[k] = dist[u] + Edge[u][k];
    				path[k] = u;
    			}//if
    		}//for
    	}//for
    }
    
    int main() {
    	int m;
    	int i, j;
    	while (scanf("%d%d", &n, &m) && n && m) {
    		for (i = 0; i < n; i++)
    			for (j = 0; j < n; j++)
    				if (i == j)
    					Edge[i][j] = 0;
    				else
    					Edge[i][j] = INF;
    		int x, y, z;
    		for (i = 0; i < m; i++) {
    			scanf("%d%d%d", &x, &y, &z);
    			if (z < Edge[x-1][y-1])
    				Edge[y-1][x-1] = Edge[x-1][y-1] = z;
    		}
    		Dijkstra(0);
    		printf("%d
    ", dist[n - 1]);
    	}//while
    	return 0;
    }




  • 相关阅读:
    mysql 5.7 安装手册(for linux)
    Git服务器分类
    Git服务器安装详解及安装遇到问题解决方案
    使用git进行版本管理
    Git 忽略一些文件不加入版本控制
    Windows下搭建基于SSH的Git服务器
    linux系统下mysql跳过密码验证登录和创建新用户
    阅读《不止代码》之心得分享
    Sonar安装和常见问题解决
    Eclipse安装Sonarlint插件
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212131.html
Copyright © 2020-2023  润新知