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. jQuery学习之prop和attr的区别示例介绍

    1..prop( propertyName ) 获取匹配集合中第一个元素的Property的值 2. .prop( propertyName, value ) .prop( map ) .prop( ...

  2. html 标签

    CSS : overflow : hidden   --  就是给一个盒子定义了一个显示范围.内部的物体.只有在这个范围内部才会被显示.不然就被隐藏. overflow-x overflow-y 控制 ...

  3. C#在二维码中添加圆角logo

    public class QRCodeHelper { #region 合并用户QR图片和用户头像 /// <summary> /// 合并用户QR图片和用户头像 /// </sum ...

  4. eclipse自动排版JSP问题

    eclipse自动排版JSP非常难看,标签每行显示不完整,开发时很难受,下面设置一下这个就好多了: window-->preferences-->Web-->HTML Files-- ...

  5. Oracle 文件的导入与导出

    说明:本机使用的是32位oracle,使用的方法是plsql导入与导出 1.导出数据步骤. 1)登陆上plsql后在工具里选择导出用户对象,选择上所有的表在选择保存的路径.点击导出就可以了. 2)上边 ...

  6. ffmpeg 常用命令

    mp4中的h264编码,而h264有两种封装: 一种是annexb模式,传统模式,有startcode,SPS和PPS是在ES中:另一种是mp4模式,一般mp4.mkv.avi会没有startcode ...

  7. url带#号,微信授权,微信分享那些坑

    微信授权的方法是,在项目里面配置拦截器(此处可以参考各个框架的拦截器)没有拦截器也可以,反正意思就是跳转到项目里的时候判断微信环境 如果是微信环境, 判断微信环境的方法是 var ua = windo ...

  8. Spring和SpringMVC父子容器关系初窥

    一.背景 最近由于项目的包扫描出现了问题,在解决问题的过程中,偶然发现了Spring和SpringMVC是有父子容器关系的,而且正是因为这个才往往会出现包扫描的问题,我们在此来分析和理解Spring和 ...

  9. iOS常用系统信息获取方法

    一.手机电量获取,方法二需要导入头文件#import<objc/runtime.h> 方法一.获取电池电量(一般用百分数表示,大家自行处理就好) -(CGFloat)getBatteryQ ...

  10. SEL-消息机制

    int main() { Person *p = [[Person alloc] init]; //调用方法 [p test2]; [p performSelector:@selector(test2 ...