Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4417 Accepted Submission(s): 2013
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
題意:求起點到各個公交站的距離和加上從各個公交站返回起點的最小距離
思路:各個公交站返回起點的最小距離可以利用反向建圖,即求反向建圖的起點到各個公交站的最小距離(多源點到單源點),因爲數據過大,否則可以利用floyd來求,下面利用dijkstra和spfa來求,(dijkstra+優先隊列在密集圖有優勢,而spfa則在稀疏圖有優勢,畢竟dijkstra複雜程度((V+E)logV),而spfa複雜程度爲(K*E),K期望值爲2)
上代碼
dijkstra
//951ms
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define Max 1000010
bool visit[Max];
int n, top, head[Max], d[Max];
int aa[Max][3];
struct node
{
int to, val, next;
}edge[Max];
void add(int a, int b, int c)
{
edge[top].to = b;
edge[top].val = c;
edge[top].next = head[a];
head[a] = top++;
}
void init()
{
memset(head, -1, sizeof(head));
top = 0;
}
int dijkstra()
{
priority_queue<pair<int, int> >PQ;
memset(visit, false, sizeof(visit));
for (int i = 1; i <= n; i++)
d[i] = inf;
d[1] = 0;
PQ.push(make_pair(0, 1));
while (!PQ.empty())
{
pair<int, int>f = PQ.top();
PQ.pop();
int u = f.second;
visit[u] = true;
if (d[u]<f.first*(-1))
continue;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (visit[v])
continue;
if (d[v]>d[u] + edge[i].val)
{
d[v] = d[u] + edge[i].val;
PQ.push(make_pair(d[v] * (-1), v));
}
}
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (d[i] != inf)
cnt += d[i];
return cnt;
}
int main()
{
//freopen("Text.txt","r",stdin);
int N, q, a, b, c;
cin >> N;
while (N--)
{
init();
int sum = 0;
scanf("%d%d", &n, &q);
for (int j = 0; j<q; j++)
{
scanf("%d%d%d", &aa[j][0], &aa[j][1], &aa[j][2]);
add(aa[j][0], aa[j][1], aa[j][2]);
}
sum += dijkstra();
init();
for (int i = 0; i<q; i++)//反向建圖
add(aa[i][1], aa[i][0], aa[i][2]);
sum += dijkstra();
printf("%d\n", sum);
}
return 0;
}
spfa
//1435ms
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define Max 1000010
bool visit[Max];
int n, top, head[Max], d[Max];
int aa[Max][3];
struct node
{
int to, val, next;
}edge[Max];
void add(int a, int b, int c)
{
edge[top].to = b;
edge[top].val = c;
edge[top].next = head[a];
head[a] = top++;
}
void init()
{
memset(head, -1, sizeof(head));
top = 0;
}
int spfa()
{
memset(visit, false, sizeof(visit));
for (int i = 1; i <= n; i++)
d[i] = inf;
queue<int>S;
S.push(1);
d[1] = 0;
visit[1] = true;
while (!S.empty())
{
int u = S.front();
S.pop();
visit[u] = false;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (d[v]>d[u] + edge[i].val)
{
d[v] = d[u] + edge[i].val;
if (!visit[v])
{
S.push(v);
visit[v] = true;
}
}
}
}
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (d[i] != inf)
cnt += d[i];
}
return cnt;
}
int main()
{
//freopen("Text.txt","r",stdin);
int N, q, a, b, c;
cin >> N;
while (N--)
{
init();
int sum = 0;
scanf("%d%d", &n, &q);
for (int j = 0; j<q; j++)
{
scanf("%d%d%d", &aa[j][0], &aa[j][1], &aa[j][2]);
add(aa[j][0], aa[j][1], aa[j][2]);
}
sum += spfa();
init();
for (int i = 0; i<q; i++)//反向建圖
add(aa[i][1], aa[i][0], aa[i][2]);
sum += spfa();
printf("%d\n", sum);
}
return 0;
}