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. 如何添加商*通新对话快捷链接?不用js代码

    我们在使用商务通一般都是在页面中嵌入一段js代码,如果您是js洁癖,是不是在想着如何直接用一张小图加上商*通新对话链接来代替呢?好,那就一起来研究一下吧. 首先,我们打开一个有商*通js弹窗的页面,比 ...

  2. Func

    Func<List<int>, string> getStr = (list) => { var returnStr = ""; if (list.A ...

  3. Java学习——连接数据库

    1.去官网下载对应版本的Ojdbc.jar(oracle).sqljdbc.jar(sqlserver). 2.放置到项目lib文件夹下 3.项目右键->Build Path->confi ...

  4. Markdown 语法简要介绍

    =================MarkDown================= Markdown 是一种方便记忆.书写的纯文本标记语言,用户可以使用这些标记符号以最小的输入代价生成极富表现力的文 ...

  5. ajax——CORS跨域调用REST API 的常见问题以及前后端的设置

    RESTful架构是目前比较流行的一种互联网软件架构,在此架构之下的浏览器前端和手机端能共用后端接口. 但是涉及到js跨域调用接口总是很头疼,下边就跟着chrome的报错信息一起来解决一下. 假设:前 ...

  6. DataSet装换为泛型集合 222

    #region DataSet装换为泛型集合 /// <summary> /// 利用反射和泛型 /// </summary> /// <param name=" ...

  7. SQL多表查询,消除表中的重复的内容

    看到朋友再写一个SQL语句:两个表a1表中有SN.SN2.TN,b1表有SM.SM2.TN2,若a1的SN中的数据和b1的SM中的数据是一致的,那么将a1中对应的数据修改成b1中对应的数据. upda ...

  8. 9. 了解 Cocoa-百度百科

    Cocoa是苹果公司为Mac OS X所创建的原生面向对象的API,是Mac OS X上五大API之一(其它四个是Carbon.POSIX.X11和Java). 苹果的面向对象开发框架,用来生成 Ma ...

  9. C#设计模式之原型模式

    原型模式:使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象. 分析: 孙悟空:根据自己的形状复制(克隆)出多个身外身 软件开发:通过复制一个原型对象得到多个与原型对象一模一样的新对 ...

  10. AngularJS 包含

    在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中,目前还不支持包含 HTML 文件的功能. 大多服务端脚本都支持包含文件功能 (SSI: Server Sid ...