Invitation Cards
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 24003 | Accepted: 7918 |
Description
In the age of television, not manypeople attend theater performances. Antique Comedians of Malidinesia are awareof this fact. They want to propagate theater and, most of all, AntiqueComedies. They have printed invitation cards with all the necessary informationand with the programme. A lot of students were hired to distribute theseinvitations among the people. Each student volunteer has assigned exactly onebus stop and he or she stays there the whole day and gives invitation to peopletravelling by bus. A special course was taken where students learned how toinfluence people and what is the difference between influencing and robbery.
The transport system is very special: all lines are unidirectional and connectexactly two stops. Buses leave the originating stop with passangers each halfan hour. After reaching the destination stop they return empty to theoriginating stop, where they wait until the next full half an hour, e.g. X:00or X:30, where 'X' denotes the hour. The fee for transport between two stops isgiven by special tables and is payable on the spot. The lines are planned insuch a way, that each round trip (i.e. a journey starting and finishing at thesame stop) passes through a Central Checkpoint Stop (CCS) where each passengerhas to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is tomove to one predetermined stop to invite passengers. There are as manyvolunteers 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 ofmoney to pay every day for the transport of their employees.
Input
The input consists of N cases. Thefirst line of the input contains only positive integer N. Then follow thecases. Each case begins with a line containing exactly two integers P and Q, 1<= P,Q <= 1000000. P is the number of stops including CCS and Q thenumber of bus lines. Then there are Q lines, each describing one bus line. Eachof the lines contains exactly three numbers - the originating stop, thedestination stop and the price. The CCS is designated by number 1. Prices arepositive integers the sum of which is smaller than 1000000000. You can alsoassume it is always possible to get from any stop to any other stop.
Output
For each case, print one linecontaining the minimum amount of money to be paid each day by ACM for thetravel costs of its volunteers.
Sample Input
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
Sample Output
46
210
Source
正着找一遍最短路 倒着再找一遍 就可以了
直接套用spfa的模板
AC代码:
#include<iostream>
#include<cstdio>
#include<memory.h>
#include<queue>
usingnamespace std;
#definemax(x,y) (x>y?x:y)
#definemin(x,y) (x<y?x:y)
#defineINF 0x3f3f3f3f
#defineMAX1 1000005
#defineMAX2 1000005
structnode
{
int u,v;
long long c;
node(){}
node(int u,int v,long long c):u(u),v(v),c(c){}
};
nodep[MAX2];
inthead[MAX1],next1[MAX2];
longlong dp[MAX1];
boolmap[MAX1];
intcnt[MAX1];
inte,top,n,m;
voidaddnode(int u,int v,long long c)
{
p[e] = node(u,v,c);
next1[e] = head[u];
head[u] = e++;
}
voidinit()
{
memset(head, -1, sizeof(head[0])*(n + 2));
memset(next1, -1, sizeof(next1[0])*(n +2));
e = 0;
for(int i = 0; i < m; i++)
{
int u,v;
long long c;
scanf("%d%d%I64d",&u,&v,&c);
addnode(u,v,c);
}
}
voidun_init()
{
memset(head,-1,sizeof(head[0])*(n + 2));
memset(next1,-1,sizeof(next1[0])*(n + 2));
e = 0;
for(int i = 0; i < m; i++)
{
addnode(p[i].v, p[i].u, p[i].c);
}
}
longlong get_sum(long long a[])
{
long long res = 0;
for(int i = 1; i <= n; i++)
res+=a[i];
return res;
}
boolrelax(int u,int v,long long c)
{
if(dp[v] > dp[u] + c)
{
dp[v] = dp[u] + c;
return true;
}
return false;
}
voidspfa(int t)
{
memset(map,0,sizeof(map[0])*(n + 2));
for(int i = 1; i <= n; i++)
dp[i] = INF;
dp[t] = 0;
map[t] = true;
top = 0;
cnt[top++] = t;
while(top)
{
int pre = cnt[--top];
map[pre] = false;
for(int j = head[pre]; j + 1; j =next1[j])
{
if(relax(p[j].u, p[j].v,p[j].c)&&map[p[j].v] == false)
{
cnt[top++] = p[j].v;
map[p[j].v] = true;
}
}
}
}
intmain()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
spfa(1);
long long ans = get_sum(dp);
un_init();
spfa(1);
ans += get_sum(dp);
printf("%I64d\n",ans);
}
return 0;
}