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最短路刷题的更多相关文章

  1. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  2. hdu 2066 最短路水题

    题意:给出多个可选择的起始点和终点,求最短路 思路:执行起始点次的spfa即可 代码: #include<iostream> #include<cstdio> #include ...

  3. HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)

    前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...

  4. 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)

    转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...

  5. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 教你用python写:HDU刷题神器

    声明:本文以学习为目的,请不要影响他人正常判题 HDU刷题神器,早已被前辈们做出来了,不过没有见过用python写的.大一的时候见识了学长写这个,当时还是一脸懵逼,只知道这玩意儿好屌-.时隔一年,决定 ...

  7. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. HDU 2544 最短路(模板题——Floyd算法)

    题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...

  9. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. 【CodeForces】914 F. Substrings in a String bitset

    [题目]F. Substrings in a String [题意]给定小写字母字符串s,支持两种操作:1.修改某个位置的字符,2.给定字符串y,查询区间[l,r]内出现y多少次.|s|,Σ|y|&l ...

  2. c/c++ const 用法

    概述 1. const有什么用途? 在 c程序中,const的用法主要有定义常量.修饰函数参数.修饰函数返回值等3个用处. 在c++程序中,它还可以修饰函数的定义体,定义类中某个成员为常态函数,即不改 ...

  3. 《区块链100问》第73集:达世币Dash是什么?

    达世币诞生于2014年1月18日,匿名程度较比特币更高. 达世币有三种转账方式,一是像比特币一样的普通转账:二是即时交易.不需要矿工打包确认,就可以确认交易,几乎可以实现秒到:三是匿名交易.从区块链上 ...

  4. spfa+floyed+最长路+差分约束系统(F - XYZZY POJ - 1932)(题目起这么长感觉有点慌--)

    题目链接:https://cn.vjudge.net/contest/276233#problem/F 题目大意:给你n个房子能到达的地方,然后每进入一个房子,会消耗一定的生命值(有可能是负),问你一 ...

  5. webSQL 增删改查

    webSQL 增删改查  转载于:https://www.cnblogs.com/liuhao-web/p/7866032.html /** *数据库操作辅助类,定义对象.数据操作方法都在这里定义 * ...

  6. [转]CNN目标检测(一):Faster RCNN详解

    https://blog.csdn.net/a8039974/article/details/77592389 Faster RCNN github : https://github.com/rbgi ...

  7. 一、springboot入门

    构建spring boot工程一般采用两种方式 gradle .maven maven方式 pom.xml spring-boot-starter:核心模块,包括自动配置支持.日志和YAML spri ...

  8. Linux下获取和设置IP

    在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作. 那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体. /* Interfa ...

  9. mybatis待研究

    1. mapper 中_parameter 的含义,是 参数? 为什么?

  10. python网络编程--线程锁(互斥锁Mutex)

    一:为什么需要线程锁 一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况? 很简单,假设你有A,B两 ...