A B E 是签到题不在说了。
C. Mr.L's Journey 9TH BUPT Collegiate Programming Contest - Preliminary - 2015
题目描述
During the summer vocation,Mr.L wants to travel to Berland.There are n cities and m bidirectional roads in that lovely country, Mr.L starts his trip at city s.For some reasons,he wants to arrive at city t as soon as possible.In his travel list, there are also k cities that he thinks are beautiful.How can he get to his destination in the fastest way with the most beautiful cities traveled in his path?You can assume that the length of each road is 1.
输入格式
The first line contains integers n, m, s, t, k (3≤n≤100,0≤m≤n∗(n−1)/2,1≤s,t≤n,0≤k≤n) separated by spaces.Then the next m lines contain m pairs of city (xi,yi) means the city xi and city yi connect by a road .The last line contains k distinct integers ki(1≤ki≤n) means the city ki is a beautiful city separated by spaces.
输出格式
Only a line contains two integers separated by spaces, the first one is the shortest length and the second is the maximum number of beautiful cities he can pass in the fastest way.It guarantees that there is always a path from s to t.
输入样例
3 2 1 3 2
1 2
2 3
1 3
输出样例
2 2
题意:给你一张图,再给出一些特殊点,然后给出一些特殊的点,让求一条如下要求的路径: 首先保证路径的长度最短,在这个前提下,经过尽可能多的特殊点。
思路:就是最短路的变式,每个点维护两个域,一个距离,一个是经过特殊点的个数,然后再更新节点的时候加入条件队列的情况有两个:
(1) 距离变小
(2) 距离相同 特殊点个数增多
剩下的就是普通的spfa了
code:
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 1e9+5;
using namespace std;
const int maxn=105;
const int maxe=30005;
struct edge
{
int to,next;
}P[maxe];
int head[maxn],si;
int dis[maxn],cnt[maxn];
bool vis[maxn],be[maxn];
int n,m,ss,tt,k;
void add(int s,int t)
{
P[si].to=t;
P[si].next=head[s];
head[s]=si++;
}
void spfa(int s,int t)
{
for(int i=0;i<=n;i++){
dis[i]=inf;
cnt[i]=0;
}
memset(vis,false,sizeof vis);
dis[s]=0;
if(be[s]) cnt[s]++;
queue<int> que;
que.push(s);
while(que.size()){
int v=que.front(); que.pop();
for(int i=head[v];i!=-1;i=P[i].next){
int u=P[i].to,mm=cnt[v];
if(be[u]) mm++;
if(dis[v]+1<dis[u]){
dis[u]=dis[v]+1;
cnt[u]=mm;
if(!vis[u]){
que.push(u);
vis[u]=true;
}
}
else if(dis[v]+1==dis[u]&&cnt[u]<mm){
cnt[u]=mm;
if(!vis[u]){
que.push(u);
vis[u]=true;
}
}
}
vis[v]=false;
}
printf("%d %d\n",dis[t],cnt[t]);
}
int main()
{
// freopen("0.txt","r",stdin);
// freopen("output.txt","w",stdout);
int st,ed;
while(scanf("%d%d%d%d%d",&n,&m,&st,&ed,&k)!=EOF){
memset(head,-1,sizeof head); si=0;
memset(be,false,sizeof be);
for(int i=0;i<m;i++){
scanf("%d%d",&ss,&tt);
add(ss,tt);
add(tt,ss);
}
for(int i=0;i<k;i++){
scanf("%d",&ss);
be[ss]=true;
}
spfa(st,ed);
}
return 0;
}
D. Lettree's Math Homework 9TH BUPT Collegiate Programming Contest - Preliminary - 2015
题目描述
Little lettree is a middle school student who is clever but hates doing homework very much.
However, on this Qingming holiday, his math teacher assigned a lot of homework about fractions just like this:
Decomposite the fraction