POJ3377

来源:互联网 发布:主持人辅助软件 编辑:程序博客网 时间:2024/06/03 06:34

1.题目描述:

Ferry Lanes
Time Limit: 2000MS Memory Limit: 131072KTotal 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

40 0 1 41 3 5 73 5 1 3 71 3 7 50

Sample Output

17

Source

POJ Monthly--2007.09.09, Dagger
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);elseq.push_back(v);}}}}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();#endifint 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_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);#endifreturn 0;}

0 0
原创粉丝点击