题目:
http://poj.org/problem?id=1724
题意:
有
n
个城市,这些城市之间有
思路:
直接
#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;
}