POJ 1511 Invitation Cards(spfa‖スタック最適化)
4349 ワード
~~~テーマリンク~~~
今また一部の人(数量と驻车駅の数量は同じです)は驻车駅までチラシを配って、それらはチラシを配り终わった后にまた公共交通バスに乗って起点1に帰って、彼らの行く时と帰る时の费用の総额を求めて最小でいくらです
spfa
dijkstar+優先キュー
今また一部の人(数量と驻车駅の数量は同じです)は驻车駅までチラシを配って、それらはチラシを配り终わった后にまた公共交通バスに乗って起点1に帰って、彼らの行く时と帰る时の费用の総额を求めて最小でいくらです
spfa
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
int v, c, next;
}edge[2*1000002];
int n = 0, q = 0, cnt = 0, head1[1000002], head2[1000002], used[1000002], dist[1000002], stack[1000002];
void addedge(int u, int v, int c)
{
edge[cnt].v = v;
edge[cnt].c = c;
edge[cnt].next = head1[u];
head1[u] = cnt++;
edge[cnt].v = u;
edge[cnt].c = c;
edge[cnt].next = head2[v];
head2[v] = cnt++;
}
void spfa(int *head)
{
int i = 0, u = 0, v = 0, c = 0, top = 0;
memset(dist, -1, sizeof(dist));
memset(used, 0, sizeof(used));
memset(stack, 0, sizeof(stack));
dist[1] = 0;
stack[top++] = 1;
while(top)
{
u = stack[--top];
used[u] = 0;
for(i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].v;
c = edge[i].c;
if(dist[v] == -1)
{
dist[v] = dist[u]+c;
stack[top++] = v;
used[v] = 1;
}
else if(dist[u]+c<dist[v])
{
dist[v] = dist[u]+c;
if(!used[v])
{
stack[top++] = v;
used[v] = 1;
}
}
}
}
}
int main()
{
int i = 0, j = 0, t = 0, u = 0, v = 0, c = 0;
__int64 ans = 0;
scanf("%d", &t);
while(t--)
{
cnt = 0;
scanf("%d %d", &n, &q);
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
for(i = 0; i<q; i++)
{
scanf("%d %d %d", &u, &v, &c);
addedge(u, v, c);
}
ans = 0;
spfa(head1);
for(i = 1; i<=n; i++)
ans += dist[i];
spfa(head2);
for(i = 1; i<=n; i++)
ans += dist[i];
printf("%I64d
", ans);
}
return 0;
}
dijkstar+優先キュー
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
int v, c, next;
}edge[2*1000002];
struct qnode
{
int v, c;
qnode(int vv, int cc): v(vv), c(cc){}
bool operator <(const qnode &r) const
{
return c>r.c;
}
};
int n = 0, q = 0, cnt = 0, head1[1000002], head2[1000002], used[1000002], dist[1000002];
void addedge(int u, int v, int c)
{
edge[cnt].v = v;
edge[cnt].c = c;
edge[cnt].next = head1[u];
head1[u] = cnt++;
edge[cnt].v = u;
edge[cnt].c = c;
edge[cnt].next = head2[v];
head2[v] = cnt++;
}
void dijkstar(int *head)
{
int i = 0, k = 1, p = 0, v = 0, c = 0;
qnode m(1, 1);
priority_queue<qnode> q;
memset(used, 0, sizeof(used));
memset(dist, -1, sizeof(dist));
used[1] = 1;
dist[1] = 0;
for(i = 1; i<=n; i++)
{
for(p = head[k]; p != -1; p = edge[p].next)
{
v = edge[p].v;
c = edge[p].c;
if(dist[v] == -1)
{
dist[v] = dist[k]+c;
q.push(qnode(v, dist[v]));
}
else if(!used[v] && dist[k]+c<dist[v])
{
dist[v] = dist[k]+c;
q.push(qnode(v, dist[v]));
}
}
if(!q.empty() && used[q.top().v])
q.pop();
if(q.empty()) return ;
m = q.top();
q.pop();
used[k = m.v] = 1;
}
}
int main()
{
int i = 0, t = 0, u = 0, v = 0, c = 0;
__int64 sum = 0;
scanf("%d", &t);
while(t--)
{
cnt = 0;
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
scanf("%d %d", &n, &q);
for(i = 0; i<q; i++)
{
scanf("%d %d %d", &u, &v, &c);
addedge(u, v, c);
}
sum = 0;
dijkstar(head1);
for(i = 1; i<=n; i++)
sum += dist[i];
dijkstar(head2);
for(i = 1; i<=n; i++)
sum += dist[i];
printf("%I64d
", sum);
}
return 0;
}