sicily 1003 Go To School
来源:互联网 发布:xp设置网络打印机共享 编辑:程序博客网 时间:2024/06/15 01:16
Lucy lives in city GZ. There are N (2 <= N && N <= 100) towns in GZ。There are M (1 <= M <= 1000) two-way roads connecting the towns, and every road has a length w (1 <= w <= 1000). Lucy’s home is in town 1, and the school is in town N. Every day, Lucy goes to school on time, she always takes the shortest path. Please write a program to calculate the length of the shortest path from town 1 to town N.
The input contains several test cases.
The first line contains two integers N, M. The following M lines each contains three integers x, y, w, meaning there is a two-way road between town x and y, and the length of the road is w.
The input will be terminated by EOF.
Output the length of the shortest path from town 1 to town N. It is guaranteed that there is at least one path from town 1 to town N.
2 12 1 4793 31 3 101 2 42 3 4
4798
这题比较传统,找最短路径,所以知道求最短路径算法的话,这题还是不难的。我用的是dijkstra算法。
#include <iostream>#include <cstring>using namespace std;int edge[110][110];int const my_MAX = 9999;int dijkstra(int s, int d){ int dis[110]; bool vis[110]; int v, i; memset(dis, my_MAX, sizeof(dis)); dis[0] = 0; dis[s] = 0; memset(vis, false, sizeof(vis)); while (true) { int min = my_MAX; for (i = 1; i <= d; i++) { if (!vis[i] && dis[i] < min) { v = i; min = dis[i]; } } if (min == my_MAX) break; vis[v] = true; dis[v] = min; for (i = 1; i <= d; i++) { if (!vis[i] && dis[i] > edge[v][i] + dis[v]) dis[i] = edge[v][i] + dis[v]; } } return dis[d];}int main(){ // freopen("input.txt","r",stdin); int n, m; while (cin >> n >> m) { int x, y, d; memset(edge, my_MAX, sizeof(edge)); for (int i = 0; i < m; i++) { cin >> x >> y >> d; edge[x][y] = d; edge[y][x] = d; } cout << dijkstra(1,n) << endl; } return 0;}
- sicily 1003 Go To School
- Go to business school -- from an engineer/entrepreneur
- Ride to School
- back to school
- back to school
- POJ1922-Ride to School
- Ride to School(水题)
- POJ1992 Ride to School
- POJ1922 Ride to School
- Ride to School
- POJ1922 Ride to School
- Ride to School
- Day1:Back to school
- S-Ride to School
- Sicily 1405. Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals
- POJ 1922 Ride to School
- fjnu 1007 Ride to School
- POJ 1922 Ride to School
- .NET FrameWork 小记 (Value Types,Refrence Types, difference)
- SQL Server 2005 没有本地发布选项
- 红帽5下Postfix的设置
- 1.使用 GridView 控件和 SqlDataSource 控件显示乐知 班学生名单
- 用Delphi实现邮件附件收发功能
- sicily 1003 Go To School
- Windows XP上的NFS服务
- 3• 使用ObjectDataSource完成上述题目 (添加select、delete、update和insert对应的类和方法)
- HTML空链接的作用
- SOAP风格中 RPC与Document的区别
- win8心得
- linux添加用户
- Linux TCP网络编程示例
- RTSP协议