zoukankan      html  css  js  c++  java
  • poj 1724 最短路+优先队列(两个约束条件)

    /*两个约束条件求最短路,用优先队列*/
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    #define N  110
    struct node {
    int u,v,w,f,next;
    }bian[N*N*4];
    int head[N],yong,money;
    void init() {
    memset(head,-1,sizeof(head));
    yong=0;
    }
    struct nodee{
      int len,w,v;
      friend bool operator<(nodee a,nodee b) {
          if(a.len==b.len)//在路程相同时,优先选择花费金钱最少的
            return a.w>b.w;
          return  a.len>b.len;
      }
    };
    void addedge(int u,int v,int w,int f) {
    bian[yong].u=u;
    bian[yong].v=v;
    bian[yong].w=w;
    bian[yong].f=f;
    bian[yong].next=head[u];
    head[u]=yong++;
    }
    int min_len(int s,int t) {
     priority_queue<nodee>q;
      int visit[N],i;
      memset(visit,0,sizeof(visit));
      nodee cur,next;
      cur.len=0;
      cur.w=0;
      cur.v=s;
      q.push(cur);
      while(!q.empty()) {
        cur=q.top();
        visit[cur.v]=1;
        q.pop();
        if(cur.v==t)
            return cur.len;
        for(i=head[cur.v];i!=-1;i=bian[i].next) {
              int v=bian[i].v;
                next.len=cur.len+bian[i].w;
                next.w=cur.w+bian[i].f;
                next.v=v;
                if(next.w<=money)
                    q.push(next);
        }
      }
      return  -1;
    }
    int main() {
        int n,m,a,b,c,d;
       while(scanf("%d%d%d",&money,&n,&m)!=EOF) {
           init();
           while(m--) {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            addedge(a,b,c,d);
         //   addedge(b,a,c,d);没看清题意建成双向边wa了很多次
           }
           printf("%d
    ",min_len(1,n));
       }
    return 0;
    }
    

  • 相关阅读:
    最大回文子串
    找出不含重复字符的最长子串的长度
    链表表示的2个数相加
    如何胜任一个小型公司的技术总监?我的感想
    React 的坑
    MobX 学习
    摘要
    AI 帮助涂鸦
    计算机的前世今生
    常用编辑器实用技巧(pycharm、sublimeText、vim、vscode、Jupyter)
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410665.html
Copyright © 2011-2022 走看看