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 ...
随机推荐
- nginx 反向代理 配置 https 实现http https同时存在 经测试 支持location 规则
server { listen ssl; #监听443端口 server_name www.app01.com; ssl on; #启用ssl加密 ssl_certificate /etc/cert/ ...
- 02、Universal app 中按钮图标使用
前言,windows10 昨天凌晨发布了,windows store 开发模型比以前的 silverlight 模型由很多优势, 我也小兴奋了一把. 正文: 在 windows phone 8.0 以 ...
- 导出word功能,用html代码在word中插入分页符
<span lang=EN-US style="font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:" mce_st ...
- Scanner.findInLine()与while的使用莫名其妙的导致NoSuchElementException: No line found
public static boolean parseHTML(Scanner sc, List<String> errorInfo) { String[] tags = new Stri ...
- eclipse配置xml的自动提示
如mybatis的mapper配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- 重置 ckeditor清空内容
转载自:http://blog.csdn.net/woshirongshaolin/article/details/8239407 <script type="text/javascr ...
- Unix系统编程()在文件特定偏移量处的IO:pread和pwrite
首先我想问的是这两个p代表的是什么? 系统调用pread和pwrite完成与read和write相类似的工作,只是前两者会在offset参数所指定的位置进行文件IO操作,而非始于文件的当前偏移量处,并 ...
- 分布式模式之Broker模式(转)
问题来源: 创建一个游戏系统,其将运行在互联网的环境中.客户端通过WWW服务或特定的客户端软件连接到游戏服务器,随着流量的增加,系统不断的膨胀,最终后台数据.业务逻辑被分布式的部署.然而相比中心化的系 ...
- linux系统中/etc/syslog.conf文件解读
1: syslog.conf的介绍 对于不同类型的Unix,标准UnixLog系统的设置,实际上除了一些关键词的不同,系统的syslog.conf格式是相同的.syslog采用可配置的.统一的系统登记 ...
- ES-PHP向ES批量添加文档报No alive nodes found in your cluster
ES-PHP向ES批量添加文档报No alive nodes found in your cluster 2016年12月14日 12:31:40 阅读数:2668 参考文章phpcurl 请求Chu ...