2020ICPC 江西省大学生程序设计竞赛 K.Travel Expense(floyed + 二分)

题目链接:https://ac.nowcoder.com/acm/contest/8827/K

题目描述
Huanhuan is always working on fansy programming questions. However, today he decided to give himself a break and travel to a beautiful country. Therefore, another problem arose.

There are totally n cities in the country. There are m two-way roads, each of them directly connects two different cities. As the country has a solid transportation system, there is always a path connects every two cities.

Huanhuan arrives at city S and wants to carry as many items as possible to city T. Everyday he will go through exactly one road. For every road he pass, a fee is to pay. Due to the policy, the fee depends on number of items you carry and the number of days you enter the country. More exactly, the fee for each road is kd, where k is the number of the items Huanghuan is to carry and d is the number of days he enter the country.

For example , Huanghuan arrives ar city 1 , and aim to city 3. The path he chooses is 1->2->3 carrying 2 items . Then the fee of road 1->2 will be 21 and the fee of road 2->3 will be 22. So the total expense is 21+22=6

Now, you are tasked to help him to decide the maximum number of items he can carry since he only have limited budget.

However, Huanhuan is prepared to travel multiple times in the future. There will be totally Q query for you.

输入描述:
Thefirst line contains two interger n,m (1≤n≤100, m≤(n(n+1)/2)),where n is number of cities and m is the number of road. (It’s guaranteed that every two cities are connected, and there are no two roads directly connects the same two cities.)
Then, m lines follow, the ith lines contains two integer ui, vi (1≤ui, vi≤n, ui≠vi), denoting the ith roads connects city ui and vi.
The next lines contains one integer Q (1≤Q≤105), denoting the number of query.
Then follows Q lines, each line contains 3 integers S, T, B (1≤S, T≤n, 0≤B≤109), denoting the city arrived, the city aimed and the budget.

输出描述:
For each query, print one integer as the maximum item Huanhuan can carry from city S to T.

示例1

输入

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

输出

5
1
2

题意

有 n 座城市,m 条双向道路,一条道路要走一天,每条道路的费用是 items ^ days ( items 是所携带的物品数量, days 是第几天),给你一个出发城市 S ,目的地城市 T ,预算 B ,问最多能携带多少物品。

分析

可以先用 floyed 算法求出各城市之间的最短距离,再二分 items 求出最大的答案。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int n,m,q;
ll dis[107][107];
ll b;
 
ll check(ll x, ll y, ll z)
{
    ll ans = 0, r = 1;
    for (int i = 1; i <= y; ++i) {
        r *= x;
        ans += r;
        if (ans > z) return ans;
    }
    return ans;
}
 
int main()
{
    memset(dis, 0x3f3f3f3f, sizeof(dis));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        dis[u][v] = dis[v][u] = 1;
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(dis[i][j] > dis[i][k] + dis[k][j])
                    dis[i][j] = dis[i][k] + dis[k][j];
     
    scanf("%d",&q);
    while(q--)
    {
        ll s,t;
        scanf("%lld%lld%lld",&s,&t,&b);
        ll tmp,mid;
        ll l = 0,r = 1000000007;
        ll ans=0;
        while(l < r)
        {
            mid = (l + r) >> 1;
            if(mid == 1) tmp = dis[s][t];
            else
            {
                tmp=check(mid,dis[s][t],b);
            }
            if(tmp<=b)ans=max(ans,mid);
            if(tmp >= b){
                r = mid;
            }
            else{
                l = mid + 1;
            }
        }
        //if(s==t)ans=0;
        //if(r * (1 - qm(r, dis[s][t])) / (1 - r) > b) r--;
        printf("%lld\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值