第九届 北邮程序设计大赛网络赛 部分题解

A B E 是签到题不在说了。


时间限制 1000 ms 内存限制 65536 KB

题目描述

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 (3n100,0mn(n1)/2,1s,tn,0kn) 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(1kin) 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;
}

时间限制 1000 ms 内存限制 65536 KB

题目描述

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
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值