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. Android Studio插件:GsonFromat

    这个Android Studio插件可以根据JSONObject格式的字符串,自动生成实体类参数. 具体见:https://github.com/zzz40500/GsonFormat

  2. Spring MVC返回Map格式JSON数据

    问题描述: ajax中走error : function(e) {} 问题背景: 在测试controller层时,试过了ResponseEntity<ResponseModel>这种类型返 ...

  3. C# WinForm实现Windows 7 Aero磨砂玻璃效果

    在Vista系统之后,微软为窗体程序提供了Aero磨砂的效果,如下图.那么用C#如 何来实现这种磨砂效果呢? 代码: using System; using System.Collections.Ge ...

  4. 《转载》Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  5. Coding List

    决定还是用回.net吧,一个人瞎搞比较快,在这里把进展做个简单的记录.

  6. DevExpress 关于alertControl 图片显示

    private void button1_Click(object sender, EventArgs e) { AlertInfo info = new AlertInfo("Captio ...

  7. iOS SpriteKit 问题

    今天偶然发现 向SKShapeNode添加子 node时,子node参考的是 SKShapeNode的parent的坐标系,但是如果使用SKSpriteNode却是使用自己的坐标系,带研究.而且sha ...

  8. 基于TXT文本的简单图书管理系统

    1 ////////////////////////////////////////////////////////////////////////////////////// //SqList.h ...

  9. 【转】实现ViewPager懒加载的三种方法

    方法一 在Fragment可见时请求数据.此方案仍预加载了前后的页面,但是没有请求数据,只有进入到当前Framgent时才请求数据. 优点:实现了数据的懒加载缺点:一次仍是三个Framgment对象, ...

  10. 在WinCC中通过VBS操作SQL Server2005

    在项目中需要在一定条件满足时,保存一些数据到数据库中,并可根据条件查询.考虑到WinCC6.2以后采用的就是SQL Server2005数据库,所以直接利用该数据库即可,通过SQL Server Ma ...