HDU 1535

Invitation Cards

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2797    Accepted Submission(s): 1327


Problem Description
In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 
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. 

 

Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. 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 the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop. 
 

Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel 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
 

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long  
using namespace std;



/************************************************

Desiner:hl
time:2015/11/09
Exe.Time:1294MS
Exe.Memory:26436K


題解:有向圖,求1點到其他點過去加回來的最短路和
兩遍SPFA算法 
************************************************/


const int MAXN=111111;
const int MAXE=111111;
const int INF=0x3f3f3f3f;
int head[MAXN],head1[MAXN];
int dist[MAXN],dist1[MAXN];
int cnt[MAXN];
int que[MAXN];
bool vis[MAXN];

struct Edge
{
    int to;
    int next;
    int v;
}edge[MAXE];
Edge edge1[MAXE];
int tol,tol1;

void init(){
	tol=0;
	memset(head,-1,sizeof(head));
	
}
void init1(){
	tol1=0;
	memset(head1,-1,sizeof(head1));
}

void add(int a,int b,int c)
{
    edge[tol].to=b;
    edge[tol].v=c;
    edge[tol].next=head[a];
    head[a]=tol++;
}

void add1(int a,int b,int c)
{
    edge1[tol1].to=b;
    edge1[tol1].v=c;
    edge1[tol1].next=head1[a];
    head1[a]=tol1++;
}

bool SPFA(int start,int n)
{
    int front=0,rear=0;
    for(int v=1;v<=n;v++)
    {
        if(v==start)
        {
            dist[v]=0;
            que[rear++]=v;
            vis[v]=true;
            cnt[v]=1;
        }
        else
        {
            dist[v]=INF;
            vis[v]=false;
            cnt[v]=0;
        }
    }
    while(front!=rear)
    {
        int u=que[front++];
        vis[u]=false;
        if(front>=MAXN)front=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].v)
            {
                dist[v]=dist[u]+edge[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    que[rear++]=v;
                    if(rear>=MAXN)rear=0;
                    if(++cnt[v]>n)return true;
                }
            }
        }
    }
    return false;
}
bool SPFA1(int start,int n)
{
    int front=0,rear=0;
    for(int v=1;v<=n;v++)
    {
        if(v==start)
        {
            dist1[v]=0;
            que[rear++]=v;
            vis[v]=true;
            cnt[v]=1;
        }
        else
        {
            dist1[v]=INF;
            vis[v]=false;
            cnt[v]=0;
        }
    }
    while(front!=rear)
    {
        int u=que[front++];
        vis[u]=false;
        if(front>=MAXN)front=0;
        for(int i=head1[u];i!=-1;i=edge1[i].next)
        {
            int v=edge1[i].to;
            if(dist1[v]>dist1[u]+edge1[i].v)
            {
                dist1[v]=dist1[u]+edge1[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    que[rear++]=v;
                    if(rear>=MAXN)rear=0;
                    if(++cnt[v]>n)return true;
                }
            }
        }
    }
    return false;
}


int main(){
	int i,j,k,l,m,n,a,b,w;
	int T;
	scanf("%d",&T); 
	while(T--){
		scanf("%d%d",&m,&n);
		init();
		init1();
		for(i=0;i<n;i++){
			scanf("%d%d%d",&a,&b,&w);
			add(a,b,w);
			add1(b,a,w);
		}
		SPFA(1,m);
		SPFA1(1,m);
		int ans=0;
		for(i=2;i<=m;i++){
			ans+=dist[i]+dist1[i];
			//printf("%d  %d||",dist[i],dist1[i]);
		}


		printf("%d\n",ans);
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章