1. 程式人生 > >poj 1724 ROADS 搜尋

poj 1724 ROADS 搜尋

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

題意:針對資料而言

5  //總花費
6  //從1——n幾個城市
7  //七條路徑(如下)
1 2 2 3 //從1到2路程為2,花費為3

求在固定花費下的最短路

深搜1

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int tot,n;
int head[1010],v[1010];
int sum;
struct p
{
    int x;
    int y;
    int len;
    int cost;
    int next;
}e[10010];
void dfs(int pos,int length,int price)
{
    if(price>tot) return; //花費太大
    if(length>sum) return; //已大於找到的最短路
    if(pos==n&&price<=tot&&sum>length)
    {
        sum=length;
        return;
    }
    for(int i=head[pos];i!=-1;i=e[i].next)
    {
        int k=e[i].y;
        if(v[k]==0)
        {
            if(price+e[i].cost<=tot)
            {
                v[k]=1; //標記
                dfs(k,length+e[i].len,price+e[i].cost);
                v[k]=0; //回溯
            }
        }

    }
}
int main()
{
    int m;
    int a,b,c,d;
    scanf("%d%d%d",&tot,&n,&m);
    int num=0;
    memset(head,-1,sizeof(head));
    memset(v,0,sizeof(v));
    sum=INF;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        e[i].x=a;
        e[i].y=b;
        e[i].len=c;
        e[i].cost=d;
        e[i].next=head[a]; //建立鄰接表
        head[a]=i;
    }
    dfs(1,0,0);
    if(sum!=INF)
        printf("%d\n",sum);
    else
        printf("-1\n");
    return 0;
}

深搜2

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std;
int tot,sum,n,v[110];
int mint[110][10010];
struct p
{
    int yy;
    int len;
    int cost;
};
vector<p>e[110];
void dfs(int x,int y,int z)
{
    if(x==n)
    {
        sum=min(sum,z);
        return;
    }
    for(int i=0; i<e[x].size(); i++)
    {
        if(v[e[x][i].yy]==0)
        {
            if(z+e[x][i].len>=sum||y+e[x][i].cost>tot||z+e[x][i].len>=mint[e[x][i].yy][e[x][i].cost+y]) //剪枝1,現在的路程已經大於等於所求得的路程,剪枝2,超過所規定的的花費,剪枝3,在規定花費內現在到這個點的路程大於以前到這個點的路程
                continue;
            mint[e[x][i].yy][e[x][i].cost+y]=z+e[x][i].len;
            v[e[x][i].yy]=1;
            dfs(e[x][i].yy,y+e[x][i].cost,z+e[x][i].len);
            v[e[x][i].yy]=0;
        }

    }
}
int main()
{
    int m,x;
    while(~scanf("%d%d%d",&tot,&n,&m))
    {
        for(int i=0; i<m; i++)
        {
            p r;
            scanf("%d%d%d%d",&x,&r.yy,&r.len,&r.cost);
            e[x].push_back(r);
        }
        sum=INF;
        memset(v,0,sizeof(v));
        memset(mint,INF,sizeof(mint));
        mint[1][0]=0; //mint陣列一維代表位置,二位代表花費,用它記錄路程
        dfs(1,0,0);//位置 花費  路程
        if(sum!=INF)
            printf("%d\n",sum);
        else
            printf("-1\n");
    }

    return 0;
}

廣搜

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int n,tot;
int v[1010],head[1010];
struct pp
{
    int x;
    int y;
    int len;
    int cost;
    int next;
}e[10010];
struct p
{
    int pos; //當前所到達的位置
    int num; //路程
    int sum; //花費 
    friend bool operator<(p x1,p y1) //優先佇列,路程從小到大排序
    {
        if(x1.sum==y1.sum)
            return x1.num>y1.num;
        return x1.sum>y1.sum;
    }
};
int bfs()
{
    p now,tmp;
    priority_queue<p>Q;
    now.pos=1;
    now.sum=0;
    now.num=0;
    v[now.pos]=1;
    Q.push(now);
    while(!Q.empty())
    {
        now=Q.top();
        Q.pop();
        if(now.pos==n)
        {
            printf("%d\n",now.sum);
            return 1;
        }
        for(int i=head[now.pos];i!=-1;i=e[i].next)
        {
            tmp.pos=e[i].y;
            if(now.num+e[i].cost<=tot)
            {
                tmp.num=now.num+e[i].cost;
                tmp.sum=now.sum+e[i].len;
                v[tmp.pos]=1;
                Q.push(tmp);
            }
        }
    }
    return 0;
}
int main()
{
    int m,a,b,c,d;
    scanf("%d%d%d",&tot,&n,&m);
    memset(v,0,sizeof(v));
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        e[i].x=a;
        e[i].y=b;
        e[i].len=c;
        e[i].cost=d;
        e[i].next=head[a]; //建立鄰接表
        head[a]=i;
    }
    if(!bfs())
        printf("-1\n");
    return 0;
}