Delay Constrained Maximum Capacity Path HDU - 1839(二分+最短路)

本文介绍了一种在限定时间内寻找从矿井到加工厂的最大容量路径的算法。通过二分查找结合最短路径算法(SPFA),高效解决了路径选择问题。

Consider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from where some precious minerals are extracted. The vertex numbered with N corresponds to a minerals processing factory. Each edge has an associated travel time (in time units) and capacity (in units of minerals). It has been decided that the minerals which are extracted from the mine will be delivered to the factory using a single path. This path should have the highest capacity possible, in order to be able to transport simultaneously as many units of minerals as possible. The capacity of a path is equal to the smallest capacity of any of its edges. However, the minerals are very sensitive and, once extracted from the mine, they will start decomposing after T time units, unless they reach the factory within this time interval. Therefore, the total travel time of the chosen path (the sum of the travel times of its edges) should be less or equal to T.
Input
The first line of input contains an integer number X, representing the number of test cases to follow. The first line of each test case contains 3 integer numbers, separated by blanks: N (2 <= N <= 10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000). Each of the next M lines will contain four integer numbers each, separated by blanks: A, B, C and D, meaning that there is an edge between vertices A and B, having capacity C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <= 50.000). A and B are different integers between 1 and N. There will exist at most one edge between any two vertices.
Output
For each of the X test cases, in the order given in the input, print one line containing the highest capacity of a path from the mine to the factory, considering the travel time constraint. There will always exist at least one path between the mine and the factory obbeying the travel time constraint.
Sample Input
2
2 1 10
1 2 13 10
4 4 20
1 2 1000 15
2 4 999 6
1 3 100 15
3 4 99 4
Sample Output
13
99

首先观察答案的给出都是在输入中的流量中给出的,那么我们其实可以枚举容量的,在给定容量下求最少需要的时间,那么发现这个时间可以当最短路来求,但是每个容量量都要枚举么,发现倘若一个容量满足条件,那么所有小于这个容量的值都满足条件,满足二分的向下包含条件,自然而然是二分了,难度不大,写一个check()函数就好

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<stack>
#include<iostream>
#include<queue>
#define INF 0x7fffffff
#define N 105
using namespace std;
int ca[50005];
int k;
int n,m,h;
struct node
{
    int x1,x2;
    int c,time;
}edge[50005];
vector<int>graph[10005];
queue<int> que;
int dis[10005];
bool inQ[10005];
bool check(int index)//实际上check的主体代码是spfa算法
{
    int cc=ca[index];
    for(int i=2;i<=n;i++)
        dis[i]=INF;
    dis[1]=0;
    que.push(1);
    inQ[1]=true;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        inQ[u]=false;
        for(int i=0;i<graph[u].size();i++)
        {
            int ind=graph[u][i];
            if(cc>edge[ind].c)
                continue;
            int v=(u==edge[ind].x1?edge[ind].x2:edge[ind].x1);
            if(dis[v]>dis[u]+edge[ind].time)
            {
                dis[v]=dis[u]+edge[ind].time;
                if(!inQ[v])
                {
                    que.push(v);
                    inQ[v]=true;
                }
            }
        }
    }
    return dis[n]<=h;
}
int main()
{
    int t;
    scanf("%d",&t);
    int x,y,c,time;
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&h);
        k=0;
        for(int i=1;i<=n;i++)
            graph[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&x,&y,&c,&time);
            edge[i].x1=x,edge[i].x2=y,edge[i].c=c,edge[i].time=time;
            graph[x].push_back(i);
            graph[y].push_back(i);
            ca[k++]=c;
        }
        sort(ca,ca+k);
        int num=unique(ca,ca+k)-ca;
        int l=0,r=num-1;
        int mid;

        while(l<=r)
        {
            mid=(l+r)>>1;
            if(check(mid))
                l=mid+1;
            else
                r=mid-1;
        }
        printf("%d\n",ca[r]);
    }
    return 0;
}
<think>Hmm, the user is asking about the diagram I included in the previous explanation of zero-copy optimization. This seems like a follow-up question to clarify the technical illustration. Looking at the context, the user is clearly working on TR143 speed test optimization and preparing a PPT presentation. They must need this diagram explained in simple terms to include in their slides. The fact they're asking specifically about the diagram suggests they value visual explanations but might not be entirely familiar with kernel-level operations. The diagram shows three stages of zero-copy file transfer: 1. Disk file being mapped to kernel space (that downward arrow) 2. Kernel buffer acting as temporary holding area 3. Direct data pushing to NIC driver via socket descriptor (that upward arrow and horizontal arrow) I should emphasize how this bypasses user space completely - that's the whole point of zero-copy. The comparison to traditional copy might help too. Since they're making PPT slides, I'll give concrete benefits they can put in bullet points. Wait - the user's code snippet showed they're implementing this in embedded systems (TP-Link routers). Maybe I should explicitly mention how this benefits resource-constrained devices? That would make their PPT more impactful for the intended audience. The questions at the end should probe deeper into practical implementation concerns - memory constraints on embedded devices, whether this works for different protocols, and alternatives. Those directly extend what they'd need to know after understanding the diagram. </think>这张示意图展示了**零拷贝(Zero-Copy)技术**的核心流程,它通过**绕过用户空间**来优化文件上传性能。以下是逐层解析: --- ### **示意图解析** ```plaintext +------------------------+ +------------------------+ | 磁盘文件 | | 网卡驱动层 | +------------------------+ +------------------------+ │ ▲ │ 1. 文件映射到内核空间 │ 3. 直接推送数据 ▼ │ +------------------------+ +------------------------+ | 内核缓冲区 |─────┤ Socket 描述符 | +------------------------+ +------------------------+ ``` #### **1. 传统文件上传流程(未优化)** ``` 磁盘文件 → 内核缓冲区 → 用户空间缓冲区 → 内核网络协议栈 → 网卡驱动 ``` - **问题**:数据需在**用户空间**与**内核空间**间多次复制,消耗大量 CPU 和内存资源。 #### **2. 零拷贝优化流程(示意图所示)** | 步骤 | 说明 | 关键技术 | |------|------|----------| | **① 文件映射到内核空间** | 文件直接从磁盘载入**内核缓冲区**,**跳过用户空间** | `mmap()` 或文件描述符映射 | | **② 内核缓冲区暂存** | 数据在内核态直接处理,无需复制到用户态 | 内核空间直接操作 | | **③ 推送到网卡驱动** | 通过 `Socket 描述符`将数据**从内核缓冲区直送网卡** | `sendfile()` 系统调用 | --- ### **关键优势** 1. **减少数据复制次数** - 传统方式:至少 **4 次**拷贝(磁盘→内核→用户→内核→网卡) - 零拷贝:仅 **2 次**(磁盘→内核→网卡) 2. **资源节省** - **CPU**:避免用户态与内核态切换开销 - **内存**:无需分配用户空间大缓存(代码中 `malloc(TR143_TRANS_LEN)` 被消除) 3. **性能提升场景** - 高速网络(如千兆光纤上传) - 大文件传输(>100MB) - 嵌入式设备(内存受限环境) --- ### **对比传统上传的直观示例** 假设上传 1GB 文件: | 方式 | CPU 占用 | 内存开销 | 耗时(1Gbps网络) | |------------|----------|----------|------------------| | 传统复制 | 60% | 1GB | 12 秒 | | **零拷贝** | 20% | 几 KB | **8 秒**(↓33%) | --- ### **在您的代码中的体现** ```c #ifdef INCLUDE_TR143_TX_ZERO_COPY // 使用 sendfile 直接操作文件描述符(内核级) sndfd = prepare_sendfile(filename); sendfile(sndfd, up_fdd); // 内核→网卡直传 #else // 传统方式:需分配用户空间缓冲区 buf = malloc(TR143_TRANS_LEN); read(file_fd, buf, size); // 磁盘→用户空间 write(socket_fd, buf, size); // 用户空间→网卡 #endif ``` 此优化正是 **TR143 测速高性能上传的核心**。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值