poj-1724(bfs+优先队列)

题意:有向图,给你m条边,每条边有两个权值,路径长和通过这条路径的花费,问你在不超过k花费的前提下,最短的路径从1走到n

解题思路:因为边数很少,我们可以直接用暴力每条边的方式来找最小的路径长,也就是用一个优先队列,每次弹出路径最短的边,计算当前花费和下条边的花费如果小于k,那么这条边就可行。

很多博客写是迪杰斯特拉+heap,我觉得没有松弛过程的应该算不上单元最短路的写法把(QAQ);

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 100000
#define inf 1e9
using namespace std;
struct Edge
{
    int next;
    int to;
    int w;
    int c;
}edge[maxn];
struct node
{
    int num;
    int dist;
    int cost;
    node(int _num=0,int _dist=0,int _cost=0):num(_num),dist(_dist),cost(_cost){}
    bool operator < (const node &a) const
	{
		return a.dist<dist;
	}

};
int head[maxn];
int cnt;
int dist[maxn];
int k,n,m;
void add(int u,int v,int w,int c)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].c=c;
    edge[cnt].next=head[u];
    head[u]=cnt++;

}
int dij(int x)
{
    int ans=-1;
    node u,v;
    priority_queue<node>que;
    que.push(node(x,0,0));
    while(!que.empty())
    {
        node u=que.top();
        que.pop();
        int now=u.num;
        if(now==n)
        {
            ans=u.dist;
            return ans;
            break;
        }
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            if(u.cost+edge[i].c<=k)
            {
                v.num=edge[i].to;
                v.dist=u.dist+edge[i].w;
                v.cost=u.cost+edge[i].c;
                que.push(v);
            }
        }
    }
    return ans;
}
int main()
{
    int x,y,w,c;

    while(cin>>k>>n>>m)
    {
        memset(head,-1,sizeof(head));
            cnt=0;
        for(int i=1;i<=m;i++)
        {
            cin>>x>>y>>w>>c;
            add(x,y,w,c);
        }
        cout<<dij(1)<<endl;
    }

}

  

原文地址:https://www.cnblogs.com/huangdao/p/9250973.html

时间: 2024-10-09 01:20:59

poj-1724(bfs+优先队列)的相关文章

POJ 3635 BFS+优先队列

Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6326   Accepted: 2086 Description After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you v

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

POJ 1724 ROADS

点击打开链接 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10202   Accepted: 3786 Description N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and t

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

深搜+剪枝 POJ 1724 ROADS

POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 Description N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an