POJ 1724 ROADS bfs || dfs || A*

来源:互联网 发布:笔记本风扇调节软件 编辑:程序博客网 时间:2024/06/07 01:36

题目:

http://poj.org/problem?id=1724

题意:

n个城市,这些城市之间有m条道路,每条道路有长度和价格两种属性,问从1n在花费不超过k的情况下的最短路径

思路:

直接dfs可以过。bfs+优先队列也可以,到达一个点时只要价格不超过k就入队,路径最短的优先出队,等到终点出队时,路径一定是最短的。用A的话,其实和k短路差不多,从小到大求出从1n的路径,直到有一个路径的花费满足要求为止
dfs:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100 + 10, M = 10000 + 10, INF = 0x3f3f3f3f;struct edge{    int to, cost, price, next;}g[M];int cnt, head[N];int ans;bool vis[N];void add_edge(int v, int u, int c, int p){    g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = head[v], head[v] = cnt++;}void dfs(int v, int t, int cost, int price, int k){    if(v == t)    {        ans = min(ans, cost); return;    }    for(int i = head[v]; i != -1; i = g[i].next)    {        int u = g[i].to;        if(! vis[u])        {            vis[u] = true;            if(price + g[i].price <= k && cost + g[i].cost < ans)                dfs(u, t, cost + g[i].cost, price + g[i].price, k);            vis[u] = false;        }    }}int main(){    int k, n, m;    while(~ scanf("%d%d%d", &k, &n, &m))    {        cnt = 0;        memset(head, -1, sizeof head);        int v, u, c, p;        for(int i = 1; i <= m; i++)        {            scanf("%d%d%d%d", &v, &u, &c, &p);            add_edge(v, u, c, p);        }        memset(vis, 0, sizeof vis);        ans = INF;        vis[1] = true;        dfs(1, n, 0, 0, k);        if(ans == INF) ans = -1;        printf("%d\n", ans);    }    return 0;}

bfs:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int N = 100 + 10, M = 10000 + 10, INF = 0x3f3f3f3f;struct edge{    int to, cost, price, next;}g[M];struct node{    int x, cost, price;    friend bool operator< (node a, node b)    {        return a.cost > b.cost;    }};int cnt, head[N];void add_edge(int v, int u, int c, int p){    g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = head[v], head[v] = cnt++;}int dijkstra(int s, int t, int k){    priority_queue<node> que;    node e, p;    e.x = s, e.cost = 0, e.price = 0;    que.push(e);    while(! que.empty())    {        p = que.top(); que.pop();        if(p.x == t) return p.cost;        for(int i = head[p.x]; i != -1; i = g[i].next)        {            if(p.price + g[i].price <= k)            {                e.x = g[i].to, e.cost= p.cost + g[i].cost, e.price = p.price + g[i].price;                que.push(e);            }        }    }    return -1;}int main(){    int n, m, k;    while(~ scanf("%d%d%d", &k, &n, &m))    {        cnt = 0;        memset(head, -1, sizeof head);        int v, u, c, p;        for(int i = 1; i <= m; i++)        {            scanf("%d%d%d%d", &v, &u, &c, &p);            add_edge(v, u, c, p);        }        printf("%d\n", dijkstra(1, n, k));    }    return 0;}

A:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef pair<int,int> pii;const int N = 100 + 10, M = 20000 + 10, INF = 0x3f3f3f3f;struct edge{    int to, cost, price, next;}g[M];struct node{    int v, g, h, price;    friend bool operator< (node a, node b)    {        if(a.g + a.h != b.g + b.h) return a.g + a.h > b.g + b.h;        else return a.g > b.g;    }};int cnt, head[N], rhead[N];int dis[N];bool vis[N];void add_edge(int v, int u, int c, int p){    g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = head[v], head[v] = cnt++;}void radd_edge(int v, int u, int c, int p){    g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = rhead[v], rhead[v] = cnt++;}void dijkstra(int s, int t){    priority_queue<pii, vector<pii>, greater<pii> > que;    memset(dis, 0x3f, sizeof dis);    memset(vis, 0, sizeof vis);    que.push(pii(0, s)), dis[s] = 0;    while(! que.empty())    {        int v = que.top().second; que.pop();        if(vis[v]) continue;        vis[v] = true;        for(int i = rhead[v]; i != -1; i = g[i].next)        {            int u = g[i].to;            if(dis[u] > dis[v] + g[i].cost)            {                dis[u] = dis[v] + g[i].cost;                que.push(pii(dis[u], u));            }        }    }}int a_star(int s, int t, int k){    priority_queue<node> que;    node e, p;    e.v = s, e.g = 0, e.h = dis[s], e.price = 0;    que.push(e);    while(! que.empty())    {        p = que.top(); que.pop();        if(p.v == t)        {            if(p.price <= k) return p.g;        }        for(int i = head[p.v]; i != -1; i = g[i].next)        {            e.v = g[i].to, e.g = p.g + g[i].cost, e.h = dis[g[i].to], e.price = p.price + g[i].price;            if(e.price > k) continue; //剪枝            que.push(e);        }    }    return -1;}int main(){    int k, n, m;    while(~ scanf("%d%d%d", &k, &n, &m))    {        cnt = 0;        memset(head, -1, sizeof head);        memset(rhead, -1, sizeof rhead);        int v, u, c, p;        for(int i = 1; i <= m; i++)        {            scanf("%d%d%d%d", &v, &u, &c, &p);            add_edge(v, u, c, p);            radd_edge(u, v, c, p);        }        dijkstra(n, 1);        int ans = a_star(1, n, k);        printf("%d\n", ans);    }    return 0;}