POJ-2387 Til the Cows Come Home
2015-08-30 21:10
288 查看
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; const int INF = 99999; int graph[maxn][maxn]; int root[maxn]; int sign[maxn]; int t, n, ans; void Dijkstra() { ans = 0; memset(sign, 0, sizeof(sign)); for(int i = 1; i <= n; i ++) root[i] = graph[1][i]; sign[1] = 1; for(int cnt = 2; cnt <= n; cnt ++) { int min_num = INF; int minn = 1; for(int i = 1; i <= n; i ++) if(!sign[i] && root[i] < min_num) { min_num = root[i]; minn = i; } sign[minn] = 1; if(min_num == INF) break; for(int i = 1; i <= n; i ++) root[i] = min(root[minn] + graph[minn][i], root[i]); } } int main() { while(~scanf("%d %d", & t, & n)) { for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) graph[i][j] = INF; while(t --) { int a, b, c; scanf("%d %d %d", & a, & b, & c); if(c < graph[a][b]) graph[a][b] = graph[b][a] = c; } Dijkstra(); printf("%d\n", root ); } return 0; }
题意:
输入t, n。n表示地标的个数。之后t行输入a b c。ab代表地标标号。c代表ab距离。求1到n的最短距离。
题解:
Dijkstra。
相关文章推荐
- linux之下的更新系统时间
- hihoCoder挑战赛14 题目1 : 不等式
- [笔记][Java7并发编程实战手册]4.5-4.6 运行多个任务并处理第一个结果/所有结果ThreadPoolExecutor
- com/opensymphony/xwork2/spring/SpringObjectFactory.java:230:-1
- 关于C#下使用轻量级数据库的一些经验
- 8月 (August)总结规划
- [c#美味] Guid ToString 格式知多少?
- 常用的 Linux命令
- XML XML常见的操作 http://blog.csdn.net/pbymw8iwm/article/details/7899156
- 交友不慎
- noip 2006
- 3 JavaWeb之Mac下Myeclipse配置Tomcat创建一个简单问web工程
- UDP服务器/客户端代码示例
- CRect::DeflateRect()
- iOS7 CookBook精彩瞬间(一)property、selector细节、__unused
- 2.2.4、容器的互联
- Opencv鼠标事件下的ROI
- iOS7 CookBook精彩瞬间(一)property、selector细节、__unused
- HDU1301 最小生成树kruskal裸题
- 树莓派安装操作系统-raspbian的方法