您的位置:首页 > 其它

HDU 2066 一个人的旅行

2010-07-04 23:55 309 查看
最短路, 求从k1个初始点 到 k2个终点中最短的一条路径长度。

设置一个超级源点 和 超级汇点即可, 使得超级源点到k1个初始点的权值为0,超级汇点. . .

下面是用priority_queue优化的Dijkstra代码:

78ms。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 1010
#define INF 0x3fffffff
int map[MAXN][MAXN];
int dis[MAXN];
bool hash[MAXN];
struct Dij
{
int id;
int dis;
bool friend operator < (const Dij a, const Dij b)
{
return a.dis > b.dis;
}
};
Dij now, next;
void Init()
{
for (int i = 0; i < MAXN; i++)
{
map[i][i] = 0;
dis[i] = INF;
for (int j = i + 1; j < MAXN; j++)
{
map[i][j] = map[j][i] = INF;
}
}
}
int Bfs(int n)
{
priority_queue<Dij> Q;
now.id = 0;
now.dis = 0;
dis[0] = 0;
Q.push(now);
memset(hash, false, sizeof(hash));
while (!Q.empty())
{
now = Q.top();
Q.pop();
if (hash[now.id])
{
continue;
}
if (now.id == n)
{
return now.dis;
}
hash[now.id] = true;

for (int j = 0; j <= n; j++)
{
if (!hash[j] && dis[now.id] + map[now.id][j] < dis[j])
{
dis[j] = dis[now.id] + map[now.id][j];
next.id = j;
next.dis = dis[j];
Q.push(next);
}
}
}
}
int main()
{
int t, s, d, a, b, c, max, ans, i, ss, dd;

while (scanf("%d %d %d", &t, &s, &d) != EOF)
{
Init();
int max = 0;
for (i = 0; i < t; i++)
{
scanf("%d %d %d", &a, &b, &c);
if (c < map[a][b])
{
map[a][b] = map[b][a] = c;
}
if (a > max)
max = a;
if (b > max)
max = b;
}
while (s--)
{
scanf("%d", &ss);
map[0][ss] = map[ss][0] = 0;
}
while (d--)
{
scanf("%d", &dd);
map[dd][max + 1] = map[max + 1][dd] = 0;
}
ans = Bfs(max + 1);
printf("%d/n", ans);
}
return 0;
}
 

下面用邻接表写的 0ms。

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 1010
#define INF 0x3fffffff
bool hash[MAXN];
struct Edge
{//边
int adj;
int weight;
Edge *next;
};
Edge *map[MAXN], vex[MAXN];//vex顶点
struct Dij
{
int id;
int dis;
bool friend operator < (const Dij a, const Dij b)
{
return a.dis > b.dis;
}
};
Dij now, next;
void Init()
{
for (int i = 0; i < MAXN; i++)
{
map[i] = &vex[i];//初始指向顶点
}
}
void Creat(int a, int b, int c)
{//建邻接表   与a邻接是b
Edge *pa = new Edge;
pa->adj = b;
pa->weight = c;
pa->next = NULL;
map[a]->next = pa;
map[a] = map[a]->next;
}
int Bfs(int st, int ed, int n)
{
memset(hash, false, sizeof(hash));
priority_queue<Dij> Q;
now.id = st;
now.dis = st;
Q.push(now);

while (!Q.empty())
{
now = Q.top();
Q.pop();
if (hash[now.id])
{
continue;
}
if (now.id == ed)
{
return now.dis;
}

hash[now.id] = true;
Edge *first = vex[now.id].next;
for (Edge *j = first; j != NULL; j = j->next)
{
if (hash[j->adj])
{
continue;
}
next.id = j->adj;
next.dis = now.dis + j->weight;
Q.push(next);
}
}
}
int main()
{
int i, n, t, s, d, ss, dd, a, b, c;
while (scanf("%d %d %d", &t, &s, &d) != EOF)
{
Init();
n = 0;
for (i = 0; i < t; i++)
{
scanf("%d %d %d", &a, &b, &c);
Creat(a, b, c);
Creat(b, a, c);

if (a > n)
{
n = a;
}
if (b > n)
{
n = b;
}
}
while (s--)
{
scanf("%d", &ss);
Creat(0, ss, 0);
Creat(ss, 0, 0);
}
while (d--)
{
scanf("%d", &dd);
Creat(dd, n + 1, 0);
Creat(n + 1, dd, 0);
}
int ans = Bfs(0, n + 1, n + 1);
printf("%d/n", ans);
}
return 0;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct null c 优化