SPFA最短路 建图模板 POJ1511&&POJ3159

本文介绍了一种利用最短路径算法来最小化每日交通费用的方法,帮助Antique Comedians of Malidinesia剧团合理分配学生志愿者到各个公交站进行宣传工作。
J - Invitation Cards
Time Limit:8000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output

46

210

需要建二次图:都是单源最短路;

#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
//const int inf=1e10;
#define inf 0xFFFFFFFF
typedef struct
{
    int w,v,next;
} node;
node edge[1000010];
int a[1000010][3];

int edgehead[1000010];
long long dis[1000010];
bool vis[1000010];
int n,m;int k;
void jia(int u,int v,int w)
{
    edge[k].v=v;
    edge[k].w=w;
    edge[k].next=edgehead[u];
    edgehead[u]=k++;
}
long long spfa()
{
    memset(vis,0,sizeof(vis));
    for(int i=2; i<=n; i++)
    {
        dis[i]=inf;
    }
    dis[1]=0;
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=edgehead[u]; i; i=edge[i].next)
        {
            int v=edge[i].v;
            int w=edge[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=true;
                }
            }
        }
    }
    long long ans=0;
    for(int i=1; i<=n; i++)
        ans+=dis[i];
    return ans;
}
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {

        
        memset(edgehead,0,sizeof(edgehead));
        memset(edge,0,sizeof(edge));
        k=1;scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]);
            jia(a[i][0],a[i][1],a[i][2]);
        }
        long long ans=spfa();
        memset(edgehead,0,sizeof(edgehead));
        memset(edge,0,sizeof(edge));
        k=1;
        for(int i=1; i<=m; i++)
        {

            jia(a[i][1],a[i][0],a[i][2]);
        }
        ans+=spfa();
        printf("%lld\n",ans);
    }
}





K - Candies
Time Limit:1500MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

Description

During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers AB and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2
1 2 5
2 1 4

Sample Output

5 
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
int edgehead[30010];
int vis[30010];
int q[30010];
int dis[30010];
typedef struct
{
    int w,v,next;
} node;
node edge[150010];
int tol;
void jia(int u,int v,int w)//加边
{
    edge[tol].w=w;
    edge[tol].v=v;
    edge[tol].next=edgehead[u];
    edgehead[u]=tol++;
}
void  spfa(int start,int n)
{
    int top=0;
    for(int i=1; i<=n; i++)
    {
        if(i==start)
        {
            q[top++]=i;
            vis[i]=true;
            dis[i]=0;
        }
        else
        {
            vis[i]=false;
            dis[i]=inf;
        }
    }
    while(top!=0)
    {
        int u=q[--top];
        vis[u]=false;
        for(int i=edgehead[u]; i; i=edge[i].next)
        {
            int v=edge[i].v;


            if(dis[v]>dis[u]+edge[i].w)
            {
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    q[top++]=v;
                }
            }
        }
    }
}
int main()
{
    int n,m;
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        tol=1;
        memset(edgehead,0,sizeof(edgehead));
        while(m--)
        {

            scanf("%d%d%d",&a,&b,&c);
            jia(a,b,c);
        }
        spfa(1,n);
        printf("%d\n",dis[n]);
    }
}






内容概要:本文深入剖析了HTTPS中SSL/TLS握手的全流程,系统讲解了HTTPS的诞生背景及其相较于HTTP在安全性上的优势,重点阐述了SSL/TLS握手各阶段的技术细节,包括ClientHello、ServerHello、证书交换、密钥交换及加密通信立等环节。文章结合加密基础概念(对称加密、非对称加密、哈希算法)和数字证书机制,解释了数据加密、身份验证与完整性保护的实现原理,并通过Wireshark抓包实例帮助读者直观理解握手过程中的数据交互。同时,归纳了常见握手失败问题及其解决方案,后对SSL/TLS未来发展趋势进行了展望,涵盖抗量子加密算法和高效协议优化方向。; 适合人群:具备基本网络和安全知识的开发人员、运维工程师、网络安全爱好者,以及希望深入理解HTTPS底层机制的技术从业者;尤其适合1-3年经验、正在向中高级岗位发展的技术人员。; 使用场景及目标:①掌握HTTPS工作原理及SSL/TLS握手全过程,理解加密通信立机制;②能够分析和排查HTTPS连接中的证书、加密套件、版本兼容等问题;③通过抓包实践提升对网络安全协议的实际分析能力;④为后续学习TLS 1.3、零RTT、前向保密等高级主题打下坚实基础; 阅读议:此资源理论与实践结合紧密,议在学习过程中同步使用Wireshark等工具进行抓包实验,对照文档中的握手阶段逐一验证各消息内容,加深对加密协商、证书验证和密钥生成过程的理解。同时关注新TLS版本的发展趋势,拓展安全视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值