UVa 11015 - 05-2 Rendezvous

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

問集合地點定在哪,可以讓所有人走的總路徑長度最小。

分析:圖論、最短路。直接利用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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 12:22:12

UVa 11015 - 05-2 Rendezvous的相关文章

uva 12723 概率dp

Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge iscomposed of N shelves, and each shelf has a number Qi (1 ≤ i ≤ N) of food. The top shelf, whereDudu is, is identified by the number 1, and the lowest is

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

UVa - 12664 - Interesting Calculator

先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row 1: button 0, 1, 2, 3, . . . , 9. Pressing each button appends that digit to the end of the display.• Row 2: button +0, +1, +2, +3, . . . , +9. Pressin

UVa 131 - The Psychic Poker Player

题目:手里有五张牌,桌上有一堆牌(五张),你可以弃掉手中的k张牌,然后从牌堆中取最上面的k个. 比较规则如下:(按优先级排序) 1.straight-flush:同花顺,牌面为T(10) - A,这里不论花色是否相同: 2.four-of-a-kind:四条,牌面有4个相同的值: 3.full-house:船牌,牌面有3个相同值,剩下2个也相同值: 4.flush:同花,五张牌的花色相同,不是同花顺: 5.straight:顺子,五张牌的值连续,A可以作为1也可以作为14: 6.three-of

UVA 11768 - Lattice Point or Not(数论)

UVA 11768 - Lattice Point or Not option=com_onlinejudge&Itemid=8&page=show_problem&category=516&problem=2868&mosmsg=Submission+received+with+ID+13823461" target="_blank" style="">题目链接 题意:给定两个点,构成一条线段.这些点都是十分

UVA 1456 六 Cellular Network

Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1456 A cellular network is a radio network made up of a number of cells each served by a base station located in the cell. The base sta

UVA - 147 - Dollars (集合上的动态规划)

UVA - 147 Dollars Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins. Write a program that will

【UVA】11464-Even Parity(二进制枚举子集)

枚举第一行的所有可能情况,之后根据上面行计算下面行(判断是否冲突),获得最终结果. 14058243 11464 Even Parity Accepted C++ 0.275 2014-08-18 05:14:15 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #inc

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d