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. Https方式使用Git@OSC设置密码的方式

    Https方式使用Git@OSC设置密码的方式 62561_silentboy Zoker3 years ago member https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密 ...

  2. 前端工具HBuilder安装Sass插件

    HBuilder自带未安装的less插件,但是sass插件却没有,需要自己安装,步骤如下: 1.安装sass,参考: http://www.w3cplus.com/sassguide/install. ...

  3. http://www.microsoft.com/en-pk/download/details.aspx?id=40762

    http://www.microsoft.com/en-pk/download/details.aspx?id=40762

  4. Burp Suite新手指南

    Burp Suite想必大家都用过,但是大家未必知道它的所有功能.因此,本文的主要目的就是尽量深入介绍各种功能.BurpSuite有以下这些功能: 截获代理– 让你审查修改浏览器和目标应用间的流量. ...

  5. Xcode LLDB 调试Tips

    1.  bt -- 显示当前线程调用堆栈 2.  e -- 执行表达式,动态修改当前线程变量的值 3.  frame variable -- 打印当前堆栈所有变量值 4.  expr -O --lan ...

  6. Erlang C1500K长连接推送服务-性能

    Whatsapp已经使用Erlang在生产环境跑到96GB内存单机 3M长连接,参加:WhatsApp的Erlang世界.毕竟业务级别能达到Whatsapp那样极少,现在只有千万级,单机太多挂一台影响 ...

  7. 用SQL SERVER取分组数据第一条:查出每个班级的成绩第一名

    create table test (id int, name ), score int, classname )); ,,'一班'); ,,'一班'); ,,'一班'); ,,'二班'); ,,'二 ...

  8. Unity3D配合AndroidStudio打包

    SET UNITY_PATH="C:\Program Files\Unity\Editor\Unity.exe" echo UNITY_PATH=%UNITY_PATH% SET ...

  9. 360随身wifi在win10中连不上网络

    找到服务"Wired AutoConfig"和"WLAN AutoConfig"项,点击"启动"按钮,确保使其正常启动. 讲本地网卡共享到移 ...

  10. Python3.5在Windows 7下连接ORACLE数据库

    1.首先需要安装好oracle数据库,本机适用plsql连接数据库正常,记录下数据库名称 2.安装cx_oracle模块 pip install cx_Oracle 3.python中引入模块 imp ...