HDU 1142 A Walk Through the Forest(记忆化DFS+DIJKS最短路算法)

本文介绍了一个关于寻找通过森林的不同路径数量的问题。主要内容包括问题描述、输入输出格式、样例及解析,同时给出了使用Dijkstra算法结合记忆化搜索的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9641    Accepted Submission(s): 3545


Problem Description
Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
 

Input
Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.
 

Output
For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
 

Sample Input

 
5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0
 

Sample Output

 
2 4
 

Source
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1217  1385  1162  1548  2544 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 100000000
#define LIM 1010
 int mp[LIM][LIM];
int n,m;
/*
题目在题意上就设置了一些小陷阱:
计数的对象不是最短路,条件是可以从A到B如果B的路径比A短(到终点),
这才是限制条件,如果我们知道每个点到终点的最短路,
那么可以想到用搜索加记忆化来实现计数问题,搜索树的构建
基于当前点与扩展子节点的关系,即d[p]>d[i]这样。

对于每个点到2的最短路,可以用D算法,也可以用BFS优先队列。。
结果存储在d[]数组中


*/
int d[LIM],vis[LIM],lowcast[LIM];
void Dijik()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)  lowcast[i]=mp[i][2];
    d[2]=0;vis[2]=1;
    int minn,k;

    for(int i=1;i<n;i++)
    {
        minn=INF;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&minn>lowcast[j])
            {
                minn=lowcast[j];
                k=j;
            }
        }

        vis[k]=1;
        d[k]=minn;

        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&lowcast[j]>mp[j][k]+d[k])
                lowcast[j]=mp[j][k]+d[k];
        }
    }
}
/*
void Dijik()
{
    for(int i=0;i<LIM;i++) vis[i]=MAX;
     queue<Node> seq;
     seq.push(Node(0,1));
     vis[1]=0;
     while(!seq.empty())
     {
        Node t=seq.front();
        seq.pop();
        if(t.id==2) continue;
        for(int i=1;i<=n;i++)
        {
            //if(mp[i][t.id]&&i==2) seq.push(Node(tmp,i))
            if(mp[i][t.id]==0||vis[t.id]+mp[t.id][i]>vis[i]) continue;
            int tmp=t.dis+mp[i][t.id];
            seq.push(Node(tmp,i));
            vis[i]=tmp;
           // if(i!=2)   vis[i]=1;
            newmp[t.id][i]=1;
           //cout<<t.id<<" "<<i<<endl;
        }
     }
}
*/
/*
int BFS()
{
    queue<int> seq;
    seq.push(1);
    int cnt=0;
    while(!seq.empty())
    {
        int t=seq.front();
        seq.pop();
        if(t==2) { cnt++;continue;}
        for(int i=1;i<=n;i++)
        {
            if(newmp[t][i]) seq.push(i);
        }
    }
    return cnt;
}
*/
int mem[LIM];
int mDFS(int p)
{
    if(p==2) return 1;
    if(mem[p]>0) return mem[p];
   // int &ans=mem[p];
   int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(mp[i][p]<INF&&d[i]<d[p])
        {
            if(mem[i]>0) ans+=mem[i];
            else  ans+=mDFS(i);
        }
    }
    ans+=mem[p];
    mem[p]=ans;
    //mem[p]=ans;
    return ans;
}

int main()
{
    while((cin>>n)&&n)
    {
        memset(mem,0,sizeof(mem));
        cin>>m;int x,y,z;

        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
        {
            if(i==j) mp[i][j]=0;
            else mp[i][j]=INF;
        }

        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            mp[x][y]=mp[y][x]=z;
        }

        Dijik();
        printf("%d\n",mDFS(1));
    }
    return 0;
}

资源下载链接为: https://pan.quark.cn/s/140386800631 通用大模型文本分类实践的基本原理是,借助大模型自身较强的理解和推理能力,在使用时需在prompt中明确分类任务目标,并详细解释每个类目概念,尤其要突出类目间的差别。 结合in-context learning思想,有效的prompt应包含分类任务介绍及细节、类目概念解释、每个类目对应的例子和待分类文本。但实际应用中,类目和样本较多易导致prompt过长,影响大模型推理效果,因此可先通过向量检索缩小范围,再由大模型做终决策。 具体方案为:离线时提前配置好每个类目的概念及对应样本;在线时先对给定query进行向量召回,再将召回结果交给大模型决策。 该方法不更新任何模型参数,直接使用开源模型参数。其架构参考GPT-RE并结合相关实践改写,加入上下文学习以提高准确度,还使用BGE作为向量模型,K-BERT提取文本关键词,拼接召回的相似例子作为上下文输入大模型。 代码实现上,大模型用Qwen2-7B-Instruct,Embedding采用bge-base-zh-v1.5,向量库选择milvus。分类主函数的作用是在向量库中召回相似案例,拼接prompt后输入大模型。 结果方面,使用ICL时accuracy达0.94,比bert文本分类的0.98低0.04,错误类别6个,处理时添加“家居”类别,影响不大;不使用ICL时accuracy为0.88,错误58项,可能与未修改prompt有关。 优点是无需训练即可有较好结果,例子优质、类目界限清晰时效果更佳,适合围绕通用大模型api打造工具;缺点是上限不高,仅针对一个分类任务部署大模型不划算,推理速度慢,icl的token使用多,用收费api会有额外开销。 后续可优化的点是利用key-bert提取的关键词,因为核心词语有时比语意更重要。 参考资料包括
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值