Code Forces 26C Dijkstra?
1 second
64 megabytes
standard input
standard output
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between
the vertex 1 and the vertex n.
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105),
where n is the number of vertices and m is
the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106),
where ai, bi are
edge endpoints and wi is
the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
1 4 3 5
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
1 4 3 5
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector> using namespace std;
const long long int INF=(long long int )1<<60;
#define MAX 100000
vector< pair<int,int> > a[MAX+5];
struct Node
{
int pos;
int value;
Node(){};
Node(int pos,int value)
{
this->pos=pos;
this->value=value;
}
friend bool operator <(Node a,Node b)
{
return a.value>b.value;
}
};
priority_queue<Node>q;
int vis[MAX+5];
long long int d[MAX+5];
int pre[MAX+5];
int n,m;
void Dijstra()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
d[i]=INF;
pre[1]=-1;
d[1]=0;
q.push(Node(1,0));
while(!q.empty())
{
Node term=q.top();
q.pop();
if(vis[term.pos]) continue;
vis[term.pos]=1; for(int i=0;i<a[term.pos].size();i++)
{
int v=a[term.pos][i].first;
int w=a[term.pos][i].second;
if(!vis[v]&&d[term.pos]+w<d[v])
{
d[v]=d[term.pos]+w;
pre[v]=term.pos;
q.push(Node(v,d[v]));
}
}
}
}
void print(int x)
{
if(x==-1)
return;
print(pre[x]);
if(x==n)
cout<<x<<endl;
else
cout<<x<<" ";
}
int main()
{ scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
a[i].clear();
int x,y,z; for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
a[x].push_back(make_pair(y,z));
a[y].push_back(make_pair(x,z));
}
Dijstra();
if(d[n]==INF)
printf("-1\n");
else
print(n);
return 0;
}
Code Forces 26C Dijkstra?的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- code forces Watermelon
/* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...
- code forces Jeff and Periods
/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ #include<cstdio> #include<iostrea ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- ctags简明用法
1.安装: sudo apt install ctags 2.生成tags文件: //进入目标目录 ctags -R . 3.在vim中设置tags的绝对目录,这样就可以在任何子目录使用了. :set ...
- github提交一个空目录
github默认不上传空目录,有的时候需要空目录来保持程序的结构. 二个小问题. 1.始终保持空目录,即时里面有文件,也全部忽略掉. 建立一个.gitignore文件放到空目录内. mkdir emp ...
- atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval ....
atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval .... 1. 原因::: 不个object ...
- Decoration5:引入swagger2进行API管理
这一部我们计划把swagger2引入到项目中,把网站的接口以文档的形式展示出来. 1.引入springfox-swagger2.springfox-swagger-ui 2.实现Swagger2 3. ...
- Js 省市联动
function cn(){ this.Items = {}; } cn.prototype.add = function(id,iArray){ this.Items[id] = iArray; } ...
- Extjs DateField Bug 当format为年月'Y-m',在当前月(30、31号)选择其他偶数月会乱跳的问题解决方案
Ext.form.WMDateField = Ext.extend(Ext.form.DateField, { safeParse : function(value, format) { if (/[ ...
- 回文树(统计所有回文串的个数) - MCCME 1750 Подпалиндромы
Подпалиндромы Problem's Link: http://informatics.mccme.ru//mod/statements/view.php?chapterid=1750# M ...
- 编译内核出现"mkimage" command not found - U-Boot images will not be built
参考链接: http://spyker729.blogspot.com/2010/07/mkimage-command-not-found-u-boot-images.html 制作uImage的工具 ...
- 【BZOJ】1034: [ZJOI2008]泡泡堂BNB(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1034 弱的比弱的强就用,强的比强的强就用: 否则弱的和强的比. 输的情况就是2n-ans(b,a), ...
- 学习:在Eclipse中用TODO标签管理任务(Task)。
1.Windows->Perferences->Java->Compile->Task Tags,我们就可以自定义任务标签,Eclipse中可以支持HIgh,Normal,Lo ...