码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1724 ROADS

时间:2014-05-07 23:42:01      阅读:617      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

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 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个城市,m条路,初始时你有k个金币,每条路都有一个长度以及经过这条路的花费,问在你能够承受的花费之内,从1号城市到n号城市之间的最短距离是多少。
可以用dfs进行递归回溯也可以用bfs优先队列,事实表明,优先队列的效率比较高。
dfs就是从1号开始进行逐层递归遍历所有情况,一旦金币数量不能满足花费,就退出递归,直到找到最短的路径。
优先队列是按照路径从小到大排序,那么没找到一个符合条件的点就加入到队列中,直至找到n号点。
//332K	94MS
#include<stdio.h>
#include<string.h>
#define M 107
#define inf 0x3f3f3f
int head[M],vis[M],nodes,minlen;
int n,m,k;
struct E
{
    int u,v,l,c,next;
} edge[M*M];
void addedge(int u,int v,int l,int c)
{
    edge[nodes].u=u;
    edge[nodes].v=v;
    edge[nodes].l=l;
    edge[nodes].c=c;
    edge[nodes].next=head[u];
    head[u]=nodes++;
}
void dfs(int city,int nowlen,int nowmoney)
{
    if(nowlen>minlen)return;
    if(city==n&&nowlen<minlen&&nowmoney>=0)
    {
        minlen=nowlen;
        return;
    }
    for(int i=head[city]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        int l=edge[i].l;
        int c=edge[i].c;
        if(!vis[v]&&nowmoney>=c)
        {
            vis[v]=1;
            dfs(v,nowlen+l,nowmoney-c);
            vis[v]=0;
        }
    }
    return;
}
int main()
{
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    nodes=0,minlen=inf;
    scanf("%d%d%d",&k,&n,&m);
    int u,v,l,c;
    while(m--)
    {
        scanf("%d%d%d%d",&u,&v,&l,&c);
        addedge(u,v,l,c);
    }
    dfs(1,0,k);
    if(minlen==inf)printf("-1\n");
    else
        printf("%d\n",minlen);
}

//1028K	32MS
#include<stdio.h>
#include<string.h>
#include<queue>
#define M 1007
using namespace std;
int head[M],nodes;
int k,n,m;
struct Q
{
    int pos,len,money;
    bool operator<(const Q t)const
    {
        return t.len<len;
    }
} ;
struct node
{
    int u,v,next,l,c;
}edge[M*M];
void addedge(int u,int v,int l,int c)
{
    edge[nodes].u=u;
    edge[nodes].v=v;
    edge[nodes].l=l;
    edge[nodes].c=c;
    edge[nodes].next=head[u];
    head[u]=nodes++;
}
int bfs(int u)
{
    priority_queue<Q>q;
    Q now,next;
    now.pos=1;
    now.len=0;
    now.money=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        int pos=now.pos,len=now.len,money=now.money;
        if(pos==n&&money<=k)return len;
        for(int i=head[pos];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            int l=edge[i].l;
            int c=edge[i].c;
            if(money+c<=k)
            {
                next.pos=v;
                next.len=len+l;
                next.money=money+c;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    scanf("%d%d%d",&k,&n,&m);
    nodes=0;
    memset(head,-1,sizeof(head));
    int u,v,l,c;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d%d",&u,&v,&l,&c);
        addedge(u,v,l,c);
    }
    int ans=bfs(1);
    printf("%d\n",ans);
}


POJ 1724 ROADS,布布扣,bubuko.com

POJ 1724 ROADS

标签:des   style   blog   class   code   tar   

原文地址:http://blog.csdn.net/crescent__moon/article/details/25224485

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!