POJ 1724 ROADS (有限制的最短路)

    xiaoxiao2021-04-17  36

    ROADS Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14840 Accepted: 5379

    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 the toll that needs to be paid for the road (expressed in the number of coins).  Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.  We want to help Bob to find  the shortest path from the city 1 to the city N  that he can afford with the amount of money he has. 

    Input

    The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.  The second line contains the integer N, 2 <= N <= 100, the total number of cities.  The third line contains the integer R, 1 <= R <= 10000, the total number of roads.  Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :  S is the source city, 1 <= S <= N  D is the destination city, 1 <= D <= N  L is the road length, 1 <= L <= 100  T is the toll (expressed in the number of coins), 0 <= T <=100 Notice that different roads may have the same source and destination cities.

    Output

    The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.  If such path does not exist, only number -1 should be written to the output. 

    Sample Input

    5 6 7 1 2 2 3 2 4 3 3 3 4 2 4 1 3 4 1 4 6 2 1 3 5 2 0 5 4 3 2

    Sample Output

    11

    题意:给你N个点,R条边,每条边有个距离dis和花费cost(单向边),求花费不超过K的1~n的最短路径

         比赛时看到K的限制想到有个K度最小生成树的模板,很长敲了一遍发现K度是限制的K条边。然后想着最短路变形也没有想到怎么改,忽然就又想到了是不是最小费用最大流,又敲了边模板还是不会改,最后也没能写出来

    思路:邻接表存图,用优先队列,bfs

    code:

    #include<stdio.h> #include<string.h> #include<iostream> #include<vector> #include<queue> #include<algorithm> using namespace std; const int N=105; int k,n,m; struct node { int to,dis,cost; bool operator < (const node &a) const { return dis>a.dis||(dis==a.dis&&cost>a.cost); } }; vector<node>G[N]; priority_queue<node>que; int bfs() { while(!que.empty()) que.pop(); node bb; bb.dis=bb.cost=0; bb.to=1; que.push(bb); while(!que.empty()) { node aa=que.top(); que.pop(); if(aa.to==n) { return aa.dis; } for(int i=0; i<G[aa.to].size(); i++) { if(aa.cost+G[aa.to][i].cost<=k) { node cc; cc.to=G[aa.to][i].to; cc.cost=aa.cost+G[aa.to][i].cost; cc.dis=aa.dis+G[aa.to][i].dis; que.push(cc); } } } return -1; } int main() { while(~scanf("%d%d%d",&k,&n,&m)) { for(int i=0; i<=n; i++) { G[i].clear(); } int a,b,c,d; while(m--) { scanf("%d%d%d%d",&a,&b,&c,&d); G[a].push_back(node {b,c,d}); } int ans= bfs(); printf("%d\n",ans); } }

    接下来要抓紧时间学搜索和图论了还有各种存图方式。学的都太差,只能敲模板题

    转载请注明原文地址: https://ju.6miu.com/read-674090.html

    最新回复(0)