Travel
The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n.
Among n(n−1) / 2 pairs of towns, m of them are connected by bidirectional highway, which needs aa minutes to travel.
The other pairs are connected by railway, which needs bb minutes to travel.
Find the minimum time to travel from town 1 to town n.
Input
The input consists of multiple tests. For each test:
The first line contains 4 integers n,m,a,bn,m,a,b (2≤n≤1e5,0≤m≤5⋅1e5,1≤a,b≤1e9).
Each of the following mm lines contains 22 integers ui,viui,vi, which denotes cities uiui and vivi are connected by highway. (1≤ui,vi≤n,ui≠vi).
Output
For each test, write 1 integer which denotes the minimum time.
Sample Input
3 2 1 3
1 2
2 3
3 2 2 3
1 2
2 3
Sample Output
2
-----------------------------------------我是分割线^_^---------------------------------
这个题就是一个求补图的最短路的很好的例子,由于在这个题中补图的权值一样,所以就可以和用
BFS求普通的最短路一样的求解了,至于结构体的方式来储存邻接表还是第一次,不过感觉挺方便的
所以以后还是尽量习惯吧,对了,还要注意理解题解中set的作用,用来筛选出与当前点未连接的点。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<sstream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 0x3f3f3f3f
#define Int __int64
#define pii pair<int,int>
#define check(x) cout<<"["<<x<<"]"<<endl;
const int MAXN = 5555555;
int head[MAXN];
bool vis[MAXN];
int dis[MAXN];
struct Node {
    int v;
    int w;
    int nxt;
} edge[MAXN];
long long a, b;
int n, m;
int edgecnt;
void Add_Edge(int u, int v) {
    edge[edgecnt].v = v;
    edge[edgecnt].w = a;
    edge[edgecnt].nxt = head[u];
    head[u] = edgecnt++;
}
long long Spfa() {
    memset(vis, false, sizeof(vis));
    memset(dis, 0x3f, sizeof(dis));//记得是初始化为无穷大= =
    dis[1] = 0;
    queue<int>q;
    while (!q.empty()) q.pop();
    vis[1] = true;
    q.push(1);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (int i = head[now]; i != -1; i = edge[i].nxt) {
            int v = edge[i].v;
            int w = edge[i].w;
            if (dis[v] > dis[now] + w) {
                dis[v] = dis[now] + w;
                if (!vis[v]) {//标记这里还是有点疑问,因为有的人不标记也行= =
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    return dis[n] < b ? dis[n] : b;
}
long long Bfs() {
    dis[n] = INF;
    set<int>st, ts;
    st.clear();
    ts.clear();
    for (int i = 2; i <= n; i++) {
        st.insert(i);
    }
    queue<int>q;
    while (!q.empty()) q.pop();
    q.push(1);
    dis[1] = 0;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (int i = head[now]; i != -1; i = edge[i].nxt) {
            int v = edge[i].v;
            if (st.count(v) == 0) {
                continue;
            }
            st.erase(v);
            ts.insert(v);
        }
        set<int>::iterator it = st.begin();
        for ( ; it != st.end(); it++) {
            q.push(*it);
            dis[*it] = dis[now] + 1;
        }
        st.swap(ts);
        ts.clear();
    }
    return dis[n] * b < a ? dis[n] * b : a;
}
int main() {
    //freopen("input.txt", "r", stdin);
    while (scanf("%d %d %lld %lld", &n, &m, &a, &b) != EOF) {
        edgecnt = 0;
        memset(head, -1, sizeof(head));
        int u, v;
        bool judge = false;
        for (int i = 0; i < m; i++) {
            scanf("%d %d", &u, &v);
            Add_Edge(u, v);
            Add_Edge(v, u);
            if ( (u == 1 && v == n) || (u == n && v == 1) ) {
                judge = true;
            }
        }
        if (judge) {
            printf("%d\n", Bfs());
        } else {
            printf("%d\n", Spfa());
        }
    }
    return 0;
}

图论 - Travel的更多相关文章

  1. poj 3229 The Best Travel Design ( 图论+状态压缩 )

    The Best Travel Design Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1359   Accepted: ...

  2. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  3. 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel

    有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...

  4. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  5. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

  6. 算法笔记_149:图论之桥的应用(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 1310 One-way traffic In a certain town there are n intersections connected ...

  7. QBXT Day 5图论相关

    图论是NOIP的一个非常重要的考点,换句话说,没有图论,NOIP的考纲就得少一大半(虽然很NOIP没有考纲) 图论这玩意吧,和数论一样是非常变态的东西,知识点又多又杂,但是好在一个事,他比较直观比较好 ...

  8. 【五一qbxt】day5 图论

    图论 学好图论的基础: 必须意识到图论hendanteng xuehuifangqi(雾 图 G = (V,E) 一般来说,图的存储难度主要在记录边的信息 无向图的存储中,只需要将一条无向边拆成两条即 ...

  9. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

随机推荐

  1. Cvim的安装与使用

    一.安装cvim插件 第一步:下载cvim的安装包 在linux系统下的浏览器firefox.chrome浏览器中打开下面链接 www.vim.org/scripts/download_script. ...

  2. 多线程AutoResetEvent

    我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作. AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. AutoRes ...

  3. Sublime text 3如何编辑less并转(编译)成css文件

    今天开始学习使用less这个强大方便的前端工具,本来是考虑用koala(专门编辑less的软件)来使用less的,但是发现sublime编辑器也可以实现对less的编译及高亮显示代码,这样既能少用一个 ...

  4. 在Xcode5中修改整个项目名

    总会遇到几个项目,在做到一半的时候被要求改项目名,网上找了下相关的资料,大多数是xcode5以前的版本,所以解决好了在这里mark一下,给需要的人. 目标为:将项目名XCD4改成xcd5. 先上结果图 ...

  5. MATLAB的PLOT函数线型设置及横坐标为字符串的代码实例

    2.横坐标为字符串的代码实例 cell={‘PLS’,’SVM’,’RF’,’NNET’,’NB’,’PLR’,’C5.0′,’PDA’,’KNN’,’GLM’,’BCT’};%分类方法yData=[ ...

  6. gen_server port 调用receive_match 问题

    问题由来 前些天对系统做了一个优化,将原来从queue 轮询刷出数据后每条消息一个 spawn 进程单独处理,修改为批量刷出.一条一条刷轮询刷 queue 存在问题:刷queue 进程太多时,占用CP ...

  7. Linux 命令备忘录(CentOS 7)

    创建目录testdir: mkdir  testdir 进入目录testdir:cd testdir 在testdir中创建空文件 1:   touch 1 在testdir中创建空文件 2:   t ...

  8. php关于ob_start('ob_gzhandler')启用GZIP压缩的bug

    如果使用ob_start("ob_gzhandler"); 则ob_clean()后面的输出将不显示,这是个bug,可以用ob_end_clean();ob_start(" ...

  9. java 深入技术七(类的结构和反射)

    1.java反射(reflect) java 类的结构 java反射机制就是把java类按结构分解,每一部分对应特定的反射类 java反射机制允许运行时加载,探知和使用在编译期间完全未知的classe ...

  10. 关于settimeout 和for循环

    for(var i=0;i<3;i++){ setTimeOut(function(){ console.log(i) },500) }; 执行结果:3,3,3 ---------------- ...