H - Have Some Cheese Bread
Coming back from Foz, Gabi and Divino were headed to the boarding gate. Gabi, hungry, realized the large quantity of Cheese Bread Houses in the way and wondered which way they should take so they can pass by the maximum number of Cheese Bread Houses. Divino, however, wasn’t that hungry, and wanted to know which way would minimize the number of Cheese Bread Houses.
Additionally, the airport has a strange property. The walkways between each terminal are unidirectional and, if there is a path leaving from Terminal A to Terminal B, there is no path from Terminal B to Terminal A.
Initially they are at Terminal 1 and the Gate is at Terminal N.
Input
The first line of input contains 3 integers, N, C and M (1 ≤ N ≤ 105, 1 ≤ C ≤ N, 1 ≤ M ≤ 105), indicating, respectively, the number of terminals (identifies from 1 to N), Cheese Bread Houses and walkways. The second line contains C integers, the indices of the terminals that have a Cheese Bread House. Finally, the following M lines contain 2 integers v and w, indicating thar there is a walkway from v to w.
Output
The output must contain a single line with 2 space separated integers Min and Max, the minimum and maximum number of Cheese Bread Houses by which they may pass by when going from Terminal 1 to Terminal N.
Example
Input
5 3 6
1 3 4
1 2
2 3
3 4
4 5
1 5
1 4
Output
1 3
题意:输入的第一行给你5个地方(1到N),有3个地方有一个面包店,有6条有向边连接这些地方。第二行表示三个面包店分别在哪个地方,然后接下来六行为有向边。让你求从1到N最少经过多少个面包店和最多经过多少个面包店。
注意: 给的边不会形成环。
思路:跑两遍最短路,有向边指向的地方假如没有面包店,这条边赋值为0。有向边指向的地方有面包店的时候,分两种情况来考虑,(1)、当求最少通过多少个面包店的时候,该条边赋值为1。(2)、当求最多通过多少个面包店的时候,该条边赋值为-1。 因为存在负边,就用spfa来跑最短路。
代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn =100000+5;
const int inf =0x3f3f3f3f;
int flag[maxn],head[maxn],d[maxn],vis[maxn],k;
int N,C,M;
queue<int>q;
void init()
{
k=0;
memset(flag,0,sizeof(flag));
memset(head,-1,sizeof(head));
}
struct Edge
{
int v,w,next;
Edge(){};
Edge(int v1,int w1,int next1)
{
v=v1,w=w1,next=next1;
}
}e[maxn];
void add_edge(int u,int v, int w)
{
e[k]=Edge(v,w,head[u]);
head[u]=k++;
}
int dijkstra(int s)
{
memset(d,inf,sizeof(d));
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
if(s==0)
flag[1]==1?d[1]=1:d[1]=0;
else
flag[1]==1?d[1]=-1:d[1]=0;
q.push(1);
vis[1]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u]; i!=-1; i=e[i].next)
{
int v=e[i].v;
int w=e[i].w;
if(s==1)
w=-w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
// printf("%d %d %d\n",u,v,d[v]);
if(!vis[v])
q.push(v);
}
}
}
return d[N];
}
int main()
{
while(scanf("%d %d %d",&N,&C,&M)!=EOF)
{
init();
int min_,max_;
int x,a,b;
for(int i=0; i<C; i++)
{
scanf("%d",&x);
flag[x]=1;
}
for(int i=0; i<M; i++)
{
scanf("%d %d",&a,&b);
flag[b]==1 ? add_edge(a,b,1):add_edge(a,b,0);
}
if(N==1)
{
flag[1]==1?printf("1 1\n") : printf("0 0\n");
continue;
}
min_=dijkstra(0);
max_=-dijkstra(1);
printf("%d %d\n",min_,max_);
}
return 0;
}