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 ...
随机推荐
- 【Android】12.6 利用Intent实现记事本功能(NotePad)
分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 这个例子演示如何实现一个简单的记事本功能. 该例子提前使用了后面章节将要介绍的SQLLite数据库. 二.示例-c ...
- windows phone 切换多语言时,商店标题显示错误的问题
前段时间,用业余时间写了一款 wp8 app(“超级滤镜”商店,中文地址:英文地址),在多语言的时候,给 app title 和 app tile title 进行多语言时(参考 MSDN),中文商店 ...
- :-1: error: [debug/moc_gotocelldialog.cpp] Error 2
环境:Qt5.3 问题描述: 我将一个已经做好可以运行的小程序拷贝了一下,放在另一个目录下打开,却无法运行,提示上面的错误,而且原程序也无法运行了.不知道为什么,还没有答案...
- android 批量上传图片
额外还需要NetUtil和服务器端UpLoadPicture package com.example.girdviewtest; import java.util.ArrayList;import j ...
- Wireshark-TCP协议分析(包结构以及连接的建立和释放)
原文:http://blog.csdn.net/ahafg/article/details/51039584 TCP:传输控制协议 TCP是一种面向连接的.可靠的.基于字节流的传输层通信协议. 面向 ...
- cocos2d-x聊天气泡
用cocos2d-x做聊天气泡在网上搜索了一下提示用CCScale9Sprite,这个类可以不缩放边角只 缩放中心,正好符合气泡的要求. 说一下思路,头像都是用cocosbuilder做的ccb,在代 ...
- android4.0.3源码之USB wifi移植心得
http://blog.csdn.net/eastmoon502136/article/details/7850157 http://forum.cubietech.com/forum.php?mod ...
- Istio流量管理实现机制深度解析
https://zhaohuabing.com/post/2018-09-25-istio-traffic-management-impl-intro/TOC 前言 Pilot高层架构 统一的服务模型 ...
- python 2.7安装某些包出现错误:"libxml/xmlversion.h:没有那个文件或目录"
解决办法: 1. ubuntu系统: 首先: apt-get install libxml2-dev sudo ln -s /usr/include/libxml2/libxml /usr/inclu ...
- 面向对象分析和设计(OOA/D)
UML不是OOA/D,也不是方法,它仅仅是一种图形表示法(表示的是OOA/D的想法),我们将在OOA/D中应用UML:分析,就是理解客户脑子中的概念,跟客户来沟通,分析出专业术语:设计,对分析出来的专 ...