问题 B: Bumped!

时间限制: 1 Sec  内存限制: 128 MB

提交: 351  解决: 44

[提交] [状态] [命题人:admin]

题目描述

Peter returned from the recently held ACM ICPC World finals only to find that his return flight was overbooked and he was bumped from the flight! Well, at least he wasn’t beat up by the

airline and he’s received a voucher for one free flight between any two destinations he wishes.

He is already planning next year’s trip. He plans to travel by car where necessary, but he may be using his free flight ticket for one leg of the trip. He asked for your help in his planning.

He can provide you a network of cities connected by roads, the amount it costs to buy gas for traveling between pairs of cities, and a list of available flights between some of those cities. Help Peter by finding the minimum amount of money he needs to spend to get from his hometown to next year’s destination!

输入

The input consists of a single test case. The first line lists five space-separated integers n, m, f, s, and t, denoting the number of cities n (0 < n ≤ 50 000), the number of roads m (0 ≤ m ≤ 150 000), the number of flights f (0 ≤ f ≤ 1 000), the number s (0 ≤ s < n) of the city in which Peter’s trip starts, and the number t (0 ≤ t < n) of the city Peter is trying to travel to. (Cities are numbered from 0 to n − 1.)

The first line is followed by m lines, each describing one road. A road description contains three space-separated integers i, j, and c (0 ≤ i, j < n, i 6= j and 0 < c ≤ 50 000), indicating there is a road connecting cities i and j that costs c cents to travel. Roads can be used in either direction for the same cost. All road descriptions are unique.

Each of the following f lines contains a description of an available flight, which consists of two space-separated integers u and v (0 ≤ u, v < n, u 6= v) denoting that a flight from city u to city v is available (though not from v to u unless listed elsewhere). All flight descriptions are unique.

输出

Output the minimum number of cents Peter needs to spend to get from his home town to the competition,using at most one flight. You may assume that there is a route on which Peter can reach his destination.

样例输入

8 11 1 0 5
0 1 10
0 2 10
1 2 10
2 6 40
6 7 10
5 6 10
3 5 15
3 6 40
3 4 20
1 4 20
1 3 20
4 7

样例输出

45

题意:n个点,m条边,f条飞行路线,s起始点,t终点

其中f条飞行路线可以使一条边的距离为0;

这题很坑,相当坑,有如下几点

1.  不管f有多少条 ,只能选一条 ,所以我们枚举每一条f边为0,去求最短路;

2. 如果f=0,别直接不跑了

3. 数据范围是long long ,如果初始化为INF 就会WA!!  太坑了! 改成9999999999或更多(在此WA了几百发,留下惨痛教训)

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> P;
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn =1000010;
using namespace std;
struct node{
    ll to;
    ll cost;
    bool operator < (node b)const{
        return cost > b.cost;
    }
};
struct edge{
    ll to;
    ll cost;
};
vector<edge> E[maxn];
int vis[maxn];
ll dis[maxn];
priority_queue<node>q;
ll x,y,z;
ll n,m,f,st,ed;

void Dijkstra(ll s){
    memset(vis,0,sizeof vis);
    for(int i = 0; i < n; i++)
        dis[i] = 9999999999;
    while(!q.empty())
        q.pop();
    dis[s] = 0;
    q.push(node{s,0});
    node tmp;
    while(!q.empty()){
        tmp = q.top();
        q.pop();
        ll u = tmp.to;
        if(vis[u])
            continue;
        vis[u] = 1;
        for(int i = 0; i < E[u].size(); i++){
            ll v = E[u][i].to;
            ll w = E[u][i].cost;
            if(!vis[v] && dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                q.push(node{v,dis[v]});
            }
        }
    }
}

int main(){
    scanf("%lld%lld%lld%lld%lld",&n,&m,&f,&st,&ed);
    while(m--){
        scanf("%lld%lld%lld",&x,&y,&z);
        E[x].pb(edge{y,z});
        E[y].pb(edge{x,z});
    }
    ll ans = 9999999999;
    Dijkstra(st);
    ans = dis[ed];
    while(f--){
        scanf("%lld%lld",&x,&y);
        E[x].pb(edge{y,0});
        Dijkstra(st);
        ans = min(ans,dis[ed]) ;
        E[x].erase(E[x].end() - 1);
    }
    printf("%lld\n",ans);
    return 0;
}
/*
6 7 2 0 5
0 1 10
0 3 5
1 4 10
2 3 5
0 5 100
4 5 5
2 5 5
0 2
1 3
*/

Bumped!【最短路】(神坑的更多相关文章

  1. upc组队赛6 Bumped!【最短路】

    Bumped! 题目描述 Peter returned from the recently held ACM ICPC World finals only to find that his retur ...

  2. Bumped! 2017 ICPC North American Qualifier Contest (分层建图+dijstra)

    题目描述 Peter returned from the recently held ACM ICPC World finals only to find that his return flight ...

  3. POJ-2387Til the Cows Come Home,最短路坑题,dijkstra+队列优化

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K       http://poj.org/problem?id=238 ...

  4. Bumped!【迪杰斯特拉消边、堆优化】

    Bumped! 题目链接(点击) Peter returned from the recently held ACM ICPC World Finals only to find that his r ...

  5. 【译】Unity3D Shader 新手教程(5/6) —— Bumped Diffuse Shader

    本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 动机 如果你满足以下条件,我建议你阅读这篇教程: 你想学习片段着色器(Fragment Shader). 你想实现 ...

  6. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  7. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  8. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  9. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

随机推荐

  1. 更改ORACLE归档路径及归档模式

    更改ORACLE归档路径及归档模式   在ORACLE10g和11g版本,ORACLE默认的日志归档路径为闪回恢复区($ORACLE_BASE/flash_recovery_area).对于这个路径, ...

  2. JDK线程池的拒绝策略

    关于新疆服务请求未带入来话原因的问题 经核查,该问题是由于立单接口内部没有成功调用接续的 “更新来电原因接口”导致的,接续测更新来电原因接口编码:NGCCT_UPDATESRFLAG_PUT ,立单接 ...

  3. 解决mapper绑定异常:nested exception is org.apache.ibatis.binding.BindingException:

    原因: 此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的.由于maven工程在默认情况下src/main/java目录下的mapper文件是不发布到targe ...

  4. Win7 搭建Linux开发环境

    Vargant Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境.它使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境. 功能特 ...

  5. 如何将本地大文件通过终端上传到linux服务器

    第一种方式:  SecureCRT下   上传文件只需在shell终端仿真器中输入命令“rz”,即可从弹出的对话框中选择本地磁盘上的文件,利用Zmodem上传到服务器当前路径下.   下载文件只需在s ...

  6. 配置DNS Server容易忽略的问题

    1.named服务启动成功,但nslookup解析报错: [root@xiamihost3 named]# service named restart 停止 named: [确定] 启动 named: ...

  7. bugfree3.0.1-修改“优先级”“严重等级”为中文

    1.进入目录C:\xampp\htdocs\bugfree\protected\models 2.打开文件 Info.php

  8. Hash算法和一致性Hash算法

    Hash算法 我们对同一个图片名称做相同的哈希计算时,得出的结果应该是不变的,如果我们有3台服务器,使用哈希后的结果对3求余,那么余数一定是0.1或者2,正好与我们之前的服务器编号相同,如果求余的结果 ...

  9. java框架之SpringBoot(2)-配置

    规范 SpringBoot 使用一个全局的配置文件,配置文件名固定为 application.properties 或 application.yml .比如我们要配置程序启动使用的端口号,如下: s ...

  10. [转] Mac系统Robot Framework环境搭建

    一.由于Mac系统下自带python,所以不需要再进行安装了 二.关闭mac电脑的sip, 1.重启 Mac并长按 Cmd + R 2.打开终端,执行csrutil disable命令 3.重启电脑 ...