Sicily 1823. Hardest Job Ever!
来源:互联网 发布:http js.zy.com 编辑:程序博客网 时间:2024/05/16 05:52
1823. Hardest Job Ever!
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
This is really hard, find the number of zeros in the number 12345678987654321!, here ‘!’ means factorial.
Well, I’m kidding. No one on earth may ever solve this successfully.
In fact, you are a spy, and you have stolen some top secret of the enemy, now you are to find a way which takes you least time to escape.
There are some many crosspoints and some many roads, even between two crosspoints there can be multiple roads. You are confused, but happily you have your laptop with you.
Input
The first line contains number T (T<=10), the number of test cases.
Each test case begins with two integers n and m (1<=n<=200, 0<=m<=10000), number of crosspoints and number of roads respectively. Next m lines each has three integers i,j,k (i!=j, 1<=k<=10000), indicating there is a road of length k connecting city i and city j.
You can assume that the crosspoints are numbered from 1 to n. 1 is your starting point and you need to go to crosspoint n.
Roads are bidirectional.
Output
For each test case, output one number, the shortest distance. If there is no path exist, output -1.
Sample Input
12 11 2 3
Sample Output
3
// Problem#: 1013// Submission#: 3160682// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>#include <cmath>using namespace std;#define MAX_POINT 205#define INF 99999999struct edge { int to, cost; edge(int t, int c) { to = t; cost = c; }};int n;vector<edge> E[MAX_POINT];int d[MAX_POINT];void DIJKSTRA(int s) { priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; for (int i = 1; i <= n; d[i++] = INF); d[s] = 0; q.push(make_pair(0, s)); while (!q.empty()) { pair<int, int> now = q.top(); q.pop(); if (d[now.second] < now.first) continue; for (int i = 0; i < E[now.second].size(); i++) { edge e = E[now.second][i]; if (d[e.to] > d[now.second] + e.cost) { d[e.to] = d[now.second] + e.cost; q.push(make_pair(d[e.to], e.to)); } } }}int main() { std::ios::sync_with_stdio(false); int caseNum; cin >> caseNum; while (caseNum--) { int m; cin >> n >> m; for (int i = 0; i < m; i++) { int from, to, cost; cin >> from >> to >> cost; E[from].push_back(edge(to, cost)); E[to].push_back(edge(from, cost)); } DIJKSTRA(1); if (d[n] == INF) cout << -1 << endl; else cout << d[n] << endl; for (int i = 0; i < n; i++) E[i].clear(); } //getchar(); //getchar(); return 0;}
- Sicily 1823. Hardest Job Ever!
- Sicily 1823 Hardest Job Ever!(单源最短路)
- 1823. Hardest Job Ever! (dijkstra)
- Sicily1823——Hardest Job Ever!
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- The Hardest Problem Ever
- HDU1048_The Hardest Problem Ever
- HDU1048 The Hardest Problem Ever
- Linux内核源代码情景分析-exit()
- MySQL 索引详解
- OC的内存管理--自动引入计数
- Bzoj-1452 Count
- 机器学习入门:牛顿法
- Sicily 1823. Hardest Job Ever!
- JS数字拼图小游戏
- Sicily 1825. Nickname
- Sicily 1013. Going Home
- 第二章作业
- Spring+Hibernate数据保存问题
- Sicily 1010. Zipper
- Sicily 1008. Translations
- hdu 4819 Mosaic(树套树型二维线段树)