Invitation card (forward and reverse mapping, running dij)

Posted by gammaman on Mon, 20 Dec 2021 07:40:03 +0100

Invitation card POJ - 1511
In the age of television, not many people took part in dramatic performances. Antique Comedians in Malidinesia know this fact. They want to promote drama and, most importantly, antique comedy. They printed an invitation card containing all the necessary information and programs. Many students are employed to distribute these invitations to people. Each student volunteer assigned a bus stop where he or she stayed all day and invited people traveling by bus. A special course was offered in which students learned how to influence others and the difference between influence and robbery.

The transportation system is very special: all lines are one-way, just connecting two stations. The bus leaves the departure station with passengers every half an hour. After arriving at the destination station, they return to the departure station empty and wait there for the next full half hour, such as X:00 or X:30, where "X" indicates hour. The transportation expenses between the two stations are given in a special form and paid on site. These routes are planned in such a way that each round trip (i.e. the journey starting and ending at the same station) passes through the central checkpoint (CCS), and each passenger must pass a comprehensive examination including physical scanning.

All ACM student members leave CCS every morning. Each volunteer will move to a predetermined site to invite passengers. The number of volunteers is as large as the site. At the end of the day, all students return to CCs. You will write a computer program to help ACM minimize the daily transportation expenses paid by employees.
input
The input consists of N cases. The first line of input contains only the positive integer n. Then according to the case. Each case starts with a line containing two integers P and Q, 1 < = P, Q < = 1000000. P is the number of stations including CCS and Q is the number of bus lines. Then there are Q lines, each describing a bus. Each line contains exactly three numbers - origin, destination and price. CCS is specified by the number 1. The price is a positive integer whose sum is less than 1000000000. You can also assume that you can always reach any other stop from any stop.
output
For each case, print a line containing the minimum amount that ACM pays for the travel expenses of its volunteers every day.
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

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=1000005;
const ll inf=0x3f3f3f3f3f3f3f3f;
int vist[maxn],cnt=0;
ll head1[maxn],head2[maxn];
ll dis1[maxn],dis2[maxn];
struct node{
	ll dian;
	ll zhi;
	friend bool operator<(const node& a,const node& b){
		return a.zhi>b.zhi;
	}
};
template<typename T>
inline void read(T &x)
{
	T s=0,f=1;
	char ch;
	ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=(s<<3)+(s<<1)+(ch^'0');
		ch=getchar();
	}
	x=f*s;
}
struct Edge{
	ll to;
	ll w;
	ll next;
}edge1[maxn],edge2[maxn];
void into()
{
	//memset(dis1,inf,sizeof(dis1));
	//memset(dis2,inf,sizeof(dis2));
	fill(dis1,dis1+maxn,inf);//Note that the double array is better assigned this way
	fill(dis2,dis2+maxn,inf);
	memset(head1,-1,sizeof(head1));
	memset(head2,-1,sizeof(head2));
}
void add_edge(int u,int v,int w)
{
	edge1[cnt].to=v;
	edge1[cnt].w=w;
	edge1[cnt].next=head1[u];
	head1[u]=cnt;
	
	edge2[cnt].to=u;
	edge2[cnt].w=w;
	edge2[cnt].next=head2[v];
	head2[v]=cnt;
	cnt++;
}
void dj1()
{
	memset(vist,0,sizeof(vist));
	dis1[1]=0;
	priority_queue<node> q;
	q.push({1,0});
	while(!q.empty()){
		node cur=q.top();
		q.pop();
		ll dang=cur.dian;
		if(vist[dang]) continue;
		vist[dang]=1;
		for(int i=head1[dang];i!=-1;i=edge1[i].next){
			ll next=edge1[i].to;
			if(!vist[next]&&dis1[dang]+edge1[i].w<dis1[next]){//Note that this point can only be updated if it has not been marked.
				dis1[next]=dis1[dang]+edge1[i].w;
				q.push({next,dis1[next]});
			}
		}
	}
}
void dj2()
{
	memset(vist,0,sizeof(vist));
	dis2[1]=0;
	priority_queue<node> q;
	q.push({1,0});
	while(!q.empty()){
		node cur=q.top();
		q.pop();
		ll dang=cur.dian;
		if(vist[dang]) continue;
		vist[dang]=1;
		for(int i=head2[dang];i!=-1;i=edge2[i].next){
			ll next=edge2[i].to;
			if(!vist[next]&&dis2[dang]+edge2[i].w<dis2[next]){
				dis2[next]=dis2[dang]+edge2[i].w;
				q.push({next,dis2[next]});
			}
		}
	}
}
int main()
{
	int t,i,j,n,m;
	ll u,v,w;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		cnt=0; 
		into();
		for(i=1;i<=m;i++){
			read(u);
			read(v);
			read(w);
			add_edge(u,v,w);
		}
		ll ans=0;
	
		dj1();
		dj2();
		for(i=1;i<=n;i++){
			ans=ans+dis1[i]+dis2[i];//The cost of departure and return add up
		}
		printf("%lld\n",ans);
	}
}

Topics: Algorithm shortest path