POJ3377 - Ferry Lanes - 构造最短路

1.题目描述:

Ferry Lanes
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 5025 Accepted: 1101

Description

Arthur lives in a small city which is partitioned into two districts, the northern and the southern, by a river flowing through. The northern and southern districts are connected by N + 1 bidirectional ferry lanes, numbered 0 to N from west to east. Each ferry lane connects two docks in separate sides of the river. No two lanes share the same dock or cross each other.

Today Arthur needs to deliver a package from one dock to another. He knows the sailing time of each ferry lane and the time cost by walking from one dock to an adjacent one along the river bank. Arthur wants to know what is the minimum time his delivery will cost.

Input

The input consists of several test cases. The first line of each consists an integer N ( 1 ≤ N ≤ 1,000,000). The second line consists of two pairs of integers describing the starting and finishing docks, where the first of each pair represents the district (0 means northern and 1 means southern) and the second represents the lane number. The third line contains N integers describing the time cost by walking between two adjacent docks on the northern bank from lane0 to laneN. The fourth line contains N + 1 integers describing the sailing time of each ferry lane. The last line contains N integers describing the time cost by walking between two adjacent docks on the southern bank from lane0 to laneNN = 0 indicates the end of input.

Output

For each test cases output one line contains the minimum time. You may assume the answer fits in a signed 64-bit integer.

Sample Input

4
0 0 1 4
1 3 5 7
3 5 1 3 7
1 3 7 5
0

Sample Output

17

Source

2.题意概述:

一条河,南北两岸各有编号为0~n的共n+1个船坞。面对面的船坞有航线相连。给出每条航线所用的时间和人沿岸走到相邻船坞所用时间,求人从起点到终点所用最小时间。

3.解题思路:

建三个边,A河岸到B河岸,A河岸相邻,B河岸相邻,然后进行spfa就行。

4.AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x7fffffff
#define maxn 1000100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
struct node
{
	int to, next;
	ll val;
} mp[6 * maxn];
int head[2 * maxn];
ll dis[2 * maxn];
bool vis[2 * maxn];
int cnt;
void init(int n)
{
	fill(head, head + n, -1);
	cnt = 0;
}
void addedge(int u, int v, ll w)
{
	mp[cnt].to = v;
	mp[cnt].val = w;
	mp[cnt].next = head[u];
	head[u] = cnt++;
}
void spfa(int sta, int n)
{
	for (int i = 0; i <= n; i++)
	{
		dis[i] = INF;
		vis[i] = 0;
	}
	deque<int> q;
	vis[sta] = 1;
	dis[sta] = 0;
	q.push_back(sta);
	while (!q.empty())
	{
		int u = q.front();
		q.pop_front();
		vis[u] = 0;
		for (int i = head[u]; i != -1; i = mp[i].next)
		{
			int v = mp[i].to;
			ll w = mp[i].val;
			if (dis[v] == INF || dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				if (!vis[v])
				{
					vis[v] = 1;
					if (!q.empty() && dis[v] <= dis[q.front()])
						q.push_front(v);
					else
						q.push_back(v);
				}
			}
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int op[2] = { 0 };
	int n;
	while (~scanf("%d", &n), n)
	{
		op[1] = n + 1;
		int a, b;
		scanf("%d%d", &a, &b);
		int sta = op[a] + b;
		scanf("%d%d", &a, &b);
		int ed = op[a] + b;
		init(2 * n + 2);
		for (int i = 0; i < n; i++)
		{
			ll w;
			scanf("%lld", &w);
			addedge(i, i + 1, w);
			addedge(i + 1, i, w);
		}
		for (int i = 0; i <= n; i++)
		{
			ll w;
			scanf("%lld", &w);
			addedge(i, i + n + 1, w);
			addedge(i + n + 1, i, w);
		}
		for (int i = n + 1; i < 2 * n + 1; i++)
		{
			ll w;
			scanf("%lld", &w);
			addedge(i, i + 1, w);
			addedge(i + 1, i, w);
		}
		spfa(sta, 2 * n + 1);
		printf("%lld\n", dis[ed]);
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值