ROADS

来源:互联网 发布:弯沉值自动计算软件 编辑:程序博客网 时间:2024/05/17 00:18
ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14806 Accepted: 5360

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
题意很好懂,就是让你在花费小于等于k的情况下找出从1~n的最短路径长度
思路:深搜找路径,但是一般情况会超时,所以要剪枝一下
#include <iostream>#include<stdio.h>#include<cstdio>#include<iostream>#include<algorithm>#include<math.h>#include<string.h>#include<map>#include<vector>#include<deque>#define ll long long#define inf 1e6#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int minl[110][10100];struct node{    int a,b,c,d;}s;vector<node>q[105];int k,n,m;int vis[105];int ans;void dfs(int f,int sum,int sum1){    if(sum>ans)//剪枝        return;        if(sum1>k)//剪枝            return ;    if(f==n&&sum1<=k&&sum<ans)    {         ans=sum;             return ;    }    for(int i=0; i<q[f].size(); i++)    {        int u=q[f][i].b;        if(vis[u]==0&&(sum1+q[f][i].d<=k))        {            if(sum+q[f][i].c>=ans||sum+q[f][i].c>=minl[u][sum1+q[f][i].d])//如果到达当前点u的路径大于等于其他点花费同样的钱到达u的路径长度,就放弃这条边。。剪枝的一个,否则会超时                continue;            sum+=q[f][i].c;            sum1+=q[f][i].d;            minl[u][sum1+q[f][i].d]=sum;            vis[u]=1;            dfs(u,sum,sum1);            vis[u]=0;            sum-=q[f][i].c;            sum1-=q[f][i].d;        }        else            continue;    }}int main(){    while(scanf("%d",&k)!=-1)    {        scanf("%d%d",&n,&m);        memset(vis,0,sizeof(vis));        for(int i=0; i<m; i++)        {            node s;            scanf("%d%d%d%d",&s.a,&s.b,&s.c,&s.d);            //if(s.a!=s.b)            q[s.a].push_back(s);        }        for(int i=0;i<110;i++)        {            for(int j=0;j<10100;j++)            {                minl[i][j]=1<<30;            }        }        int sum=0;        int sum1=0;        vis[1]=1;        ans=inf;        dfs(1,0,0);          if(ans>=inf)         printf("-1\n");            else        printf("%d\n",ans);    }}
#include<stdio.h>#include<string.h>const int N=1010;const int INF=1e6;struct node{    int a,b,c,d;    int net;} s[10*N];int n,m,cont,head[N],vis[N],k;int ans;void we(int a,int b,int c,int d){    s[cont].a=a;    s[cont].b=b;    s[cont].c=c;    s[cont].d=d;    s[cont].net=head[a];    head[a]=cont++;}void dfs(int f,int sum,int sum1){    if(sum>ans)        return;        if(sum1>k)            return ;    if(f==n&&sum1<=k&&sum<ans)        ans=sum;    for(int i=head[f]; i!=-1; i=s[i].net)    {        int u=s[i].b;        if(vis[u]==0&&(sum1+s[i].d<=k))        {            vis[u]=1;            dfs(u,sum+s[i].c,sum1+s[i].d);            vis[u]=0;        }    }}int main(){    int i,a,b,c,d;    while(~scanf("%d%d%d",&k,&n,&m))    {        cont=0;        memset(head,-1,sizeof(head));        for(int i=0; i<m; i++)        {            scanf("%d%d%d%d",&a,&b,&c,&d);            we(a,b,c,d);        }        ans=INF;        memset(vis,0,sizeof(vis));        dfs(1,0,0);        printf("%d\n",ans<INF?ans:-1);    }}
spfa:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#define N1 101#define N2 10001#define INF 0x7ffffffusing namespace std;bool inque[N1][N2];int sum[N1][N2];class num{public:    int id,take;    num(int l,int r):id(l),take(r) {}};struct link{    int e,take,len,next;} a[N2];int b[N1],k,n,m,Top;int main(){    void addeage(int s,int d,int l,int t);    int spfa();    while(scanf("%d %d %d",&k,&n,&m)!=EOF)    {        Top = 0;        memset(b,-1,sizeof(b));        for(int i=0; i<=m-1; i++)        {            int s,d,l,t;            scanf("%d %d %d %d",&s,&d,&l,&t);            addeage(s,d,l,t);        }        int t=spfa();        printf("%d\n",t);    }    return 0;}void addeage(int s,int d,int l,int t){    a[Top].e = d;    a[Top].len = l;    a[Top].take = t;    a[Top].next = b[s];    b[s] = Top;    Top++;}int spfa(){    memset(inque,false,sizeof(inque));    for(int i = 1; i <= n; i++)    {        for(int j=0; j <= k; j++)        {            sum[i][j] = INF;        }    }    queue<num> que;    que.push(num(1,0));    inque[1][0] = true;    sum[1][0] = 0;    while(!que.empty())    {        num tag =  que.front();        int id1 = tag.id;        int take1 = tag.take;        que.pop();        inque[id1][take1] = false;        for(int i = b[id1]; i!=-1; i=a[i].next)        {            int id2 = a[i].e;            int len2 = a[i].len;            int take2 = a[i].take;            if(take1+take2<=k&&sum[id2][take1+take2]>sum[id1][take1]+len2)            {                sum[id2][take1+take2]=sum[id1][take1]+len2;                if(!inque[id2][take1+take2])                {                    que.push(num(id2,take1+take2));                    inque[id2][take1+take2] = true;                }            }        }    }    int Min = INF;    for(int i=0; i<=k; i++)    {        Min = min(sum[n][i],Min);    }    if(Min==INF)    {        return -1;    }    return Min;}


                                             
0 0
原创粉丝点击