3889: [Usaco2015 Jan]Cow Routing
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 236 Solved: 88
[Submit][Status][Discuss]
Description
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.
Input
Output
Sample Input
3 5
1 2 3 4 5
2 3
3 5 4
1 2
1 5
Sample Output
裸双键值最短路
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=3010,M=5001000;
const ll inf=0X3f3f3f3f3f3f3f3fll;
int ecnt,last[N];
struct EDGE{int to,nt,val1,val2;}e[M];
inline void add(int u,int v,int val1,int val2)
{e[++ecnt]=(EDGE){v,last[u],val1,val2};last[u]=ecnt;}
ll dis1[N],dis2[N];
int S,T,U[N];
int q[N];
bool inq[N];
void spfa()
{
memset(dis1,0X3f,sizeof(dis1));memset(dis2,0X3f,sizeof(dis2));
register int i,u,head=0,tail=1;
q[0]=S;inq[S]=1;dis1[S]=dis2[S]=0;
while(head^tail)
{
u=q[head++];inq[u]=0;tail%=N;
for(i=last[u];i;i=e[i].nt)
if(dis1[e[i].to]>dis1[u]+e[i].val1||(dis1[e[i].to]==dis1[u]+e[i].val1&&dis2[e[i].to]>dis2[u]+e[i].val2))
{
dis1[e[i].to]=dis1[u]+e[i].val1;
dis2[e[i].to]=dis2[u]+e[i].val2;
if(!inq[e[i].to])inq[e[i].to]=1,q[tail++]=e[i].to,tail%=N;
}
}
}
int main()
{
S=read();T=read();int m=read();
register int i,j,k,v,num;
for(i=1;i<=m;++i)
{
v=read();num=read();
for(j=1;j<=num;++j)
{
U[j]=read();
for(k=1;k<j;++k)add(U[k],U[j],v,j-k);
}
}
spfa();
if(dis1[T]>=inf){puts("-1 -1");return 0;}
print(dis1[T]);putchar(' ');print(dis2[T]);puts("");
return 0;
}
/*
3 4 3
3 5
1 2 3 4 5
2 3
3 5 4
1 2
1 5
2 2
*/