图论 - Travel

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;
}
时间: 2024-10-10 03:30:43

图论 - Travel的相关文章

图论及其应用——树

在之前初步介绍图的文章中,我们得知,图(graph)是表征事物之间关系的一种抽象化表示方法,而基于图的概念,我们将所有的无回路无向图拿出来,给它们一个新的名字——树.  关于树的概念性术语很多,这里我们先就简单的二叉树(一个根至多有两个子树)来进行分析.    这就是一些简单的二叉树,这里A.B.C等我们成为节点.A叫做根节点,它的两个分支是B和C,并且我们称B是一个左子树,C是一个右子树,知道了这些简单的概念,我们就可以初步的探讨一些问题了. 二叉树树作为一种特殊的图,我们也要研究其遍历的方式

POJ 3352 Road Construction(图论-tarjan)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8647   Accepted: 4318 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

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

目录 1 问题描述 2 解决方案   1 问题描述 1310 One-way traffic In a certain town there are n intersections connected by two- and one-way streets. The town is very modern so a lot of streets run through tunnels or viaducts. Of course it is possible to travel between

POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)

链接:http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BE

图论trainning-part-1 E. Invitation Cards

E. Invitation Cards Time Limit: 8000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Java class name: Main In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. Th

图论——最短路①

RT 找工就业 Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D (1 <= D <= 1,000) dollars in a city before they must work in another

爬格子呀9.17(图论)

刘汝佳的紫书差不多就告一段落吧,我觉得可以了,怎么说呢,这书也陪着自己走了一年多了吧,也目睹了从一个啥也不会的萌新到一个稍微会一点的萌新的转变. 差不多开始下本书吧,自己也大三了,时间真的有点紧啊woctm 最喜欢的图论作为自己对这本书的谢幕,也好,也好 uva10735(欧拉回路+网络流) 题意:给一个图,图中有部分是向边,部分是无向边,要求判断是否存在欧拉回路,若存在,输出路径 解法: 1 #include<bits/stdc++.h> 2 #define REP(i, a, b) for

第九节 图论和搜索

1. 图论算法(用BFS,DFS) 拓扑排序 克隆图 找连通块 六度问题 2.BFS 队列实现:    树中的BFS与图中的BFS有什么不同?树中没有环,图中有环需要一个set来记录搜索过的节点: 应用:图的遍历,最短路径 3 搜索   搜索题的套路比较固定.

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h