POJ 1724

来源:互联网 发布:浙江省电子商务大数据 编辑:程序博客网 时间:2024/06/05 01:19
ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14969 Accepted: 5414

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

5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2

Sample Output

11

Source

CEOI 1998
题意即从1号点走到n号点的最短路径,每条路有过路费,我就有k个金币,问我在金币足够的情况下,从1到达n的最短路径。
深搜加减枝,我对减枝理解的不深,第一次交TLE
AC代码
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;int head[3000000];int minn=3000000;int k,n,r;struct Edge{    int u;    int v;    int l;    int c;    int next;};Edge edge[30009];int dfs(int beg,int end,int cost,int length,int num){    if(cost>k)    {        return minn;    }    if(length>minn)    {        return minn;    }    if(num>n)    {        return minn ;    }    if(beg==end)    {        if(length<minn )        {            minn =length;            return minn;        }    }    for(int i=head[beg];i;i=edge[i].next)    {        if(edge[i].c+cost<=k)        {            cost+=edge[i].c;            length+=edge[i].l;         //   vist[edge[i].v]=1;            dfs(edge[i].v,n,cost,length,++num);         //   vist[edge[i].v]=0;            length-=edge[i].l;            cost-=edge[i].c;        }    }   return minn;}int main(){    while(~scanf("%d%d%d",&k,&n,&r))    {        memset(head,-1,sizeof(head));        for(int i=1;i<=r;i++)        {            int a,b,c,d;            scanf("%d%d%d%d",&a,&b,&c,&d);            edge[i].u=a;            edge[i].v=b;            edge[i].l=c;            edge[i].c=d;            edge[i].next=head[a];            head[a]=i;        }        dfs(1,n,0,0,1);        if(minn==3000000)        {            cout<<"-1"<<endl;        }        else        {            cout<<minn<<endl;        }    }}
然后看下大佬的代码发现我的代码是多么丑陋
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int vis[105];int first[105];struct node{    int v,le,co;    int next;}g[100010];int len;int sum;int n,m,t;const int inf=0x3f3f3f;void add_edge(int u,int v,int le,int co){    g[len].v=v;    g[len].co=co;    g[len].le=le;    g[len].next=first[u];    first[u]=len++;}void dfs(int x,int step,int cost){    if(cost>n||step>sum)        return ;    if(x==m)    {        sum=min(sum,step);    }    for(int i=first[x];i!=-1;i=g[i].next)    {        int v=g[i].v;        if(vis[v]==0)        {        vis[v]=1;        dfs(v,step+g[i].le,cost+g[i].co);        vis[v]=0;        }    }}int main(){    len=0;    scanf("%d%d%d",&n,&m,&t);    int u,v,w,l;    memset(first,-1,sizeof(first));    for(int i=0;i<t;i++)    {       scanf("%d%d%d%d",&u,&v,&w,&l);       add_edge(u,v,w,l);    }    sum=inf;    dfs(1,0,0);    if(sum==inf)        printf("-1\n");    else        printf("%d\n",sum);    return 0;}


0 0