zoukankan      html  css  js  c++  java
  • UVa 11015

    題目:有一個班級的學生要一起寫作業,所以他們要到一個統一的地點。現在給你他們各自的位置,

                問集合地點定在哪,能够讓全部人走的總路徑長度最小。

    分析:圖論、最短路。直接利用Floyd計算最短路,找到和值最小的輸出就可以。

    說明:又是太長時間沒刷題了。╮(╯▽╰)╭。

    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int dist[23][23]; 
    
    int main()
    {
    	int 	n, m, u, v, d, cases = 1;
    	string  place;
    	while (cin >> n >> m && n+m) {
    		map<int, string>nameList;
    		for (int i = 0; i < n; ++ i) {
    			cin >> place;
    			nameList[i] = place;
    		}
    		
    		for (int i = 0; i < n; ++ i) {
    			for (int j = 0; j < n; ++ j)
    				dist[i][j] = 500000;
    			dist[i][i] = 0;
    		}
    		for (int i = 0; i < m; ++ i) {
    			cin >> u >> v >> d;
    			dist[u-1][v-1] = dist[v-1][u-1] = d;
    		}
    		
    		for (int k = 0; k < n; ++ k)
    			for (int i = 0; i < n; ++ i)
    				for (int j = 0; j < n; ++ j)
    					if (dist[i][j] > dist[i][k]+dist[k][j])
    						dist[i][j] = dist[i][k]+dist[k][j];
    		int space = 0, max = 500000;
    		for (int i = 0; i < n; ++ i) {
    			int sum = 0;
    			for (int j = 0; j < n; ++ j)
    				if (i != j)
    					sum += dist[i][j];
    			if (max > sum) {
    				max = sum;
    				space = i;
    			}
    		}
    		
    		cout << "Case #" << cases ++ << " : " << nameList[space] << endl;
    	}
        return 0;
    }
    


  • 相关阅读:
    PHP
    PHP
    PHP
    网站页面引导操作
    Solr与Tomcat的整合
    POI操作文档内容
    HashTable和HashMap的区别
    ArrayList、LinkedList、HashMap底层实现
    正则表达式语法
    Java并发编程:线程间通信wait、notify
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7073160.html
Copyright © 2011-2022 走看看