hdu-3790最短路刷题
title: hdu-3790最短路刷题
date: 2018-10-20 14:50:31
tags:
- acm
- 刷题
categories: - ACM-最短路
概述
一道最短路的水题,,,尽量不看以前的代码打出来,,,熟悉一下dijkstra的格式和链式前向星的写法,,,,
虽然是水题,,,但是一开始没考虑取费用最短的wa了一发,,,,QAQ
分析
链式前向星存图,,再加一个数组保存源点到每个点的费用cst[maxm],,,注意取最少的费用
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int maxn = 1e3 + 10;
const int maxm = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int head[maxm << 1];
bool vis[maxn];
int dis[maxm];
int cst[maxm];
int cnt;
int n , m;
struct edge
{
int to;
int w;
int c;
int last;
}edge[maxm << 1];
void addedge(int u , int v , int w , int c)
{
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].c = c;
edge[cnt].last = head[u];
head[u] = cnt++;
}
struct node
{
int u;
int w;
node(int _u , int _w):u(_u) , w(_w){}
bool operator < (const node &res) const
{
return w > res.w;
}
};
void dijkstra(int n , int s)
{
for(int i = 1; i <= n; ++i)
dis[i] = (i == s) ? 0 : inf;
memset(cst , inf , sizeof cst);cst[s] = 0;
memset(vis , false , sizeof vis);
priority_queue<node> q;
while(!q.empty()) q.pop();
q.push(node(s , 0));
while(!q.empty())
{
node x = q.top();q.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = true;
for(int i = head[u] ; ~i; i = edge[i].last)
{
int to = edge[i].to;
int w = edge[i].w;
int c = edge[i].c;
if(!vis[to] && dis[u] + w <= dis[to])
{
dis[to] = dis[u] + w;
//if(cst[u] + c < cst[to])
cst[to] = cst[u] + c;
q.push(node(to , dis[to]));
}
}
}
}
int main()
{
while(scanf("%d%d" , &n , &m) && n && m)
{
cnt = 0;
memset(head , -1 , sizeof head);
int u , v , w , c;
for(int i = 1; i <= m; ++i)
{
scanf("%d%d%d%d" , &u , &v , &w , &c);
addedge(u , v , w , c);
addedge(v , u , w , c);
}
int s , t;
scanf("%d%d" , &s , &t);
dijkstra(n , s);
printf("%d %d\n" , dis[t] , cst[t]);
}
}
//最短路相等时注意取费用最短的
//
//5 7
//1 2 5 5
//2 3 4 5
//1 3 4 6
//3 4 2 2
//3 5 4 7
//4 5 2 4
//1 3 4 4
//1 5
//8 10
差不多记住了的dijkatra的代码,,,继续继续
(end)
hdu-3790最短路刷题的更多相关文章
- HDU 2544 最短路(模板题)
求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...
- hdu 2066 最短路水题
题意:给出多个可选择的起始点和终点,求最短路 思路:执行起始点次的spfa即可 代码: #include<iostream> #include<cstdio> #include ...
- HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)
前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...
- 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)
转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 教你用python写:HDU刷题神器
声明:本文以学习为目的,请不要影响他人正常判题 HDU刷题神器,早已被前辈们做出来了,不过没有见过用python写的.大一的时候见识了学长写这个,当时还是一脸懵逼,只知道这玩意儿好屌-.时隔一年,决定 ...
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- 【刷题】HDU 2222 Keywords Search
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
随机推荐
- VBS 重启 TP-Link 路由器
分享一个自己用的小工具,重启TP-Link路由器的,好像还是大学时候写的,献丑了. 其他路由器可能有些不同,但是思路都是差不多的. user = "admin" '路由器帐号 pa ...
- Linux/Unix 下自制番茄钟
习惯使用番茄工作法,在Linux上工作时也需要一个番茄钟. 安装一个Linux下番茄钟工作软件? 其实根本没必要,我们可以用Linux下经典的at命令实现一个简单的番茄钟. 安装AT 一般Linux基 ...
- 回顾一些较简单的dp题
1.导弹拦截 (+贪心) 两问:一个导弹拦截系统最多能拦多少导弹 要拦截所有导弹至少需要多少拦截系统 第一问感觉是一个比较巧妙的方法: 维护一个单调递减的序列 length[] 记录的是拦截导弹的高 ...
- C# Json字符串反序列化
using DevComponents.DotNetBar; using MyControl; using Newtonsoft.Json; using System; using System.Co ...
- 【API】Mysql UDF BackDoor
1.MySQL UDF是什么 UDF是Mysql提供给用户实现自己功能的一个接口,为了使UDF机制起作用,函数必须用C或C ++编写,并且操作系统必须支持动态加载.这篇文章主要介绍UDF开发和利用的方 ...
- 【Educational Codeforces Round28】
咸鱼选手发现自己很久不做cf了,晚节不保. A.Curriculum Vitae 枚举一下间断点的位置. #include<bits/stdc++.h> using namespace s ...
- re-sign重签名
准备: ① re-sign.jar重签名工具:(下载地址为:http://troido.de/downloads/category/1): ② 从D:\Android\sdk\build-tools\ ...
- 关于vc++ 6.0 编译器,点打开文件时自动关闭
装好VC++ 6.0后,点打开文件时编译器会自动关闭掉,然后在网上找到各位大神写的资料,果然是因为之前有安装vs2010冲突的缘故,然后http://download.csdn.net/source/ ...
- keras LSTM中间的dropout
TM有三个 model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2)) 第一个dropout是x和hidden之间的dropout,第二个是hid ...
- bootstrap File Input 多文件上传插件使用记录(二)删除原文件
在上一篇文章中,主要介绍了file input插件的初始化和多文件同步上传到服务器的相关配置等.这篇主要介绍file input插件的编辑等. 使用场景: 在后台管理框架中,一条数据中包含不固定的多张 ...