列表

详情


NC24161. [USACO 2015 Jan S]Cow Routing

描述

Tired of the cold winter weather on her farm, Bessie the cow plans to fly to a warmer destination for vacation. Unfortunately, she discovers that only one airline, Air Bovinia, is willing to sell tickets to cows, and that these tickets are somewhat complicated in structure. Air Bovinia owns N planes (1 <= N <= 1000), each of which flies on a specific "route" consisting of two or more cities. For example, one plane might fly on a route that starts at city 1, then flies to city 5, then flies to city 2, and then finally flies to city 8. No city appears multiple times in a route. If Bessie chooses to utilize a route, she can board at any city along the route and then disembark at any city later along the route. She does not need to board at the first city or disembark at the last city. Each route has a certain cost, which Bessie must pay if she uses any part of the route, irrespective of the number of cities she visits along the route. If Bessie uses a route multiple times during her travel (that is, if she leaves the route and later comes back to use it from antoher city), she must pay for it each time it is used. Bessie would like to find the cheapest way to travel from her farm (in city A) to her tropical destination (city B). Please help her decide what is the minimum cost she must pay, and also the smallest number of individual flights she must use take to achieve this minimum cost.

输入描述

The first line of input contains A, B, and N, separated by spaces.
The next 2N lines describe the available routes, in two lines per
route. The first line contains the cost of using the route (an integer
in the range 1..1,000,000,000), and the number of cities along the
route (an integer in the range 1..100). The second line contains a
list of the cities in order along the route. Each city is identified
by an integer in the range 1..1000. Note that the cost of an
itinerary can easily add up to more than can fit into a 32-bit
integer, so you should probably use 64-bit integers (e.g., "long long"
integers in C/C++).

输出描述

Output the minimum cost of an itinerary that Bessie can use to travel
from city A to city B, as well as the minimum number of individual
flights required to achieve this minimum cost. If there is no
solution, output "-1 -1" (quotes for clarity) on a single line.

示例1

输入:

3 4 3
3 5
1 2 3 4 5
2 3
3 5 4
1 2
1 5

输出:

2 2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 33ms, 内存消耗: 12280K, 提交时间: 2020-09-12 10:52:28

#include<bits/stdc++.h>
#define inf 10000000000000000
#define maxn 1010
using namespace std;
int b[maxn][maxn],d[maxn],q[maxn],c[maxn];
long long dis[maxn],a[maxn][maxn];
bool vis[maxn];
int n,m,num,s,t,mx;
inline int read() {
	int x=0,f=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-')f=-1; c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x*f;
}
void spfa() {
	for(int i=1;i<=mx;i++) dis[i]=inf;
	dis[s]=0; d[s]=0;
	int l=0,r=1;
	q[1]=s; vis[s]=1;
	while(l!=r) {
		l++;
		if(l==maxn) l=0;
		int x=q[l];
		for(int i=1;i<=mx;i++)
		  if(dis[x]+a[x][i]<dis[i]||(dis[x]+a[x][i]==dis[i]&&d[x]+b[x][i]<d[i])) {
		  	dis[i]=dis[x]+a[x][i];
		  	d[i]=d[x]+b[x][i];
		  	if(!vis[i]) {
		  		r++; 
				if(r==maxn) r=0;
		  		q[r]=i; vis[i]=1;
		  	}
		  }
		vis[x]=0;
	}
}
int main() {
	for (int i=1;i<=1000;i++)
	 for (int j=1;j<=1000;j++) a[i][j]=inf;
	s=read(); t=read(); n=read();
	for(int i=1;i<=n;i++) {
		int len=read(),cnt=read();
		for(int j=1;j<=cnt;j++) {c[j]=read(); mx=max(mx,c[j]);}
		for(int j=1;j<cnt;j++)
		  for(int k=j+1;k<=cnt;k++)
		    if(len<a[c[j]][c[k]]||(len==a[c[j]][c[k]]&&k-j<b[c[j]][c[k]])) {
		    	a[c[j]][c[k]]=len; 
				b[c[j]][c[k]]=k-j;
		    }
	}
	spfa();
	if(dis[t]!=inf) printf("%lld %d",dis[t],d[t]);
	else printf("-1 -1");
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 41ms, 内存消耗: 12428K, 提交时间: 2020-09-23 19:13:03

#include<cmath>
#include<cstdio>
#include<cstring>
#include<memory.h>
#include<iostream>
#include<algorithm>
using namespace std;
int A,B,T,t,i,j,k,n,a[101],h[1001][1001];
long long value,f[1001][1001],dis[1001],sum[1001],minn;
bool vis[1001];
int main()
{
	scanf("%d%d%d",&A,&B,&T);
	memset(f,-1,sizeof(f));
	for(t=1;t<=T;t++)
	{
		scanf("%lld%d",&value,&n);
		for(i=1;i<=n;i++) scanf("%d",&a[i]);
		for(i=1;i<n;i++)
		for(j=i+1;j<=n;j++)
		if (f[a[i]][a[j]]==-1||f[a[i]][a[j]]>=value)
		{
			if (f[a[i]][a[j]]==value)
				h[a[i]][a[j]]=min(h[a[i]][a[j]],j-i);
			else
				h[a[i]][a[j]]=j-i;
			f[a[i]][a[j]]=value;
		}
	}
	memset(dis,127,sizeof(dis));
	vis[A]=true;dis[A]=0;
	for(j=1;j<1000;j++){
		for(i=1;i<=1000;i++)
			if (f[A][i]!=-1)
			if(dis[i]>=dis[A]+f[A][i])
			{
				if (dis[i]==dis[A]+f[A][i])
					sum[i]=min(sum[i],sum[A]+h[A][i]);
				else
					sum[i]=sum[A]+h[A][i];
				dis[i]=dis[A]+f[A][i];
			}
		minn=1e12;k=0;
		for(i=1;i<=1000;i++)
			if(dis[i]<minn&&vis[i]==false)
				k=i,minn=dis[i];
		A=k;vis[k]=true;
		if (k==B||k==0) break;
		}
	if (sum[B]!=0)
		printf("%lld %d\n",dis[B],sum[B]);
	else
		printf("-1 -1\n");
}

上一题