您的位置:首页 > 其它

poj 1724 ROADS(dfs)||(优先队列+dijkstra)

2017-04-13 21:55 351 查看

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

ps:比赛时没有想到用二维数组建立dis,所以写了发DFS竟然神奇的没有超时。。。

代码(dfs):

#include<stdio.h>
#include<string.h>

#define min(a,b) (a<b?a:b)
const int inf=0x3f3f3f3f;
const int maxn=105;
int vis[maxn],first[maxn];
int n,m,k,ans,len;
struct node
{
int v,dis,cost,next;
}q[10010];

void dfs(int x,int step,int cost)
{
if(cost>k||step>ans)
return ;
if(x==n)
ans=min(ans,step);
for(int i=first[x];i!=-1;i=q[i].next)
{
int v=q[i].v;
if(!vis[v])
{
vis[v]=1;
dfs(v,step+q[i].dis,cost+q[i].cost);
vis[v]=0;
}
}
}

void add_egde(int u,int v,int dis,int cost)
{
q[len].v=v,q[len].dis=dis,q[len].cost=cost;
q[len].next=first[u];
first[u]=len++;
}

int main()
{
memset(first,-1,sizeof(first));
len=0;
scanf("%d%d%d",&k,&n,&m);
int u,v,dis,cost;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&dis,&cost);
add_egde(u,v,dis,cost);
}
ans=inf;
dfs(1,0,0);
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
return 0;
}


比赛后听别人说可以用二维数组和优先队列写,于是写了一发

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

#define maxn 105
#define maxv 10005
const int inf=0x3f3f3f3f;
struct node
{
int v,dis,cost,next;
} G[maxv];

struct Node
{
int u,dis,cost;
Node(int U,int DIS,int COST)
{
u=U,dis=DIS,cost=COST;
}
bool operator < (const Node&x)const
{
return dis>x.dis;
}
};
int first[maxn],d[maxn][maxv];//d[i][j]表示从点1到点i花费j的路程
int n,m,k,len,ans;

void dijkstra()
{
for(int i=1; i<=n; ++i)
for(int j=0; j<=k; ++j)
d[i][j]=inf;
d[1][0]=0;
priority_queue<Node>Q;
Q.push(Node(1,0,0));
while(!Q.empty())
{
Node q=Q.top();
Q.pop();
if(q.cost>k||q.dis>d[q.u][q.cost])
continue;
if(q.u==n)
{
printf("%d\n",q.dis);
return ;
}
for(int i=first[q.u]; i!=-1; i=G[i].next)
{
int v=G[i].v,dis=G[i].dis,cost=G[i].cost+q.cost;
if(d[v][cost]>q.dis+dis)
{
d[v][cost]=q.dis+dis;
Q.push(Node(v,d[v][cost],cost));
}
}
}
printf("-1\n");
}

void add_egde(int u,int v,int dis,int cost)
{
G[len].v=v,G[len].dis=dis,G[len].cost=cost;
G[len].next=first[u];
first[u]=len++;
}

int main()
{
memset(first,-1,sizeof(first));
scanf("%d%d%d",&k,&n,&m);
int u,v,dis,cost;
len=0;
for(int i=1; i<=m; ++i)
{
scanf("%d%d%d%d",&u,&v,&dis,&cost);
add_egde(u,v,dis,cost);
}
ans=inf;
dijkstra();
return 0;
}


总结:最短路的思维还是不行啊,一直不能够灵活运用,都是照搬以前的套路。。。

还是要多刷题,每次写题前要先分析这类题的套路!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: