HDU 1535 Invitation Cards

http://acm.hdu.edu.cn/showproblem.php?pid=1535

Invitation Cards

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


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
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1217  1531  1546  1384  1245 
这道题目很有意思。边数比较大。用spfa做的,这是我第一次用spfa.做这道题的时候,照模板超的。还好过了。。网上的模板真是千奇百怪啊。
题目大意就是求从起点1出发到标号2~n的目的地的费用加上从2~n回到起点1的费用。所以要正向建图然后spfa,再反向建图spfa。为什么这样做就行?看图 hdu1535-Invitation Cards - 风未定 - Guanjun的博客~~
 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>


#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
const int maxn=1000002;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
struct node
{
int v,w,next;
}edge1[maxn],edge2[maxn];
int pre1[maxn];
int pre2[maxn];
int n,m;
int dist[maxn];
int vis[maxn];
ll ans;
void init()
{
memset(pre1,-1,sizeof(pre1));
memset(pre2,-1,sizeof(pre2));
int index1=1,index2=1;
int i,j,flag;
int x,y,w;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
edge1[index1].v=y;
edge1[index1].w=w;
edge1[index1].next=pre1[x];
pre1[x]=index1++;
edge2[index2].v=x;
edge2[index2].w=w;
edge2[index2].next=pre2[y];
pre2[y]=index2++;
}
}
void spfa(int *pre,node *edge)
{
int start=1;
//int end=n;
queue <int>q;
cle(vis);
fill(dist+1,dist+1+maxn,INF);
dist[start]=0;
vis[start]=1;
q.push(start);
while(!q.empty())
{
int top=q.front();///边的起点
q.pop();
vis[top]=0;
for(int j=pre[top];j!=-1;j=edge[j].next)
{
int e=edge[j].v;///边的终点
if(dist[e]>edge[j].w+dist[top])
{
dist[e]=edge[j].w+dist[top];
if(!vis[e])
{
q.push(e);
vis[e]=1;
}
}
}
}
for(int i=2;i<=n;i++)
ans+=dist[i];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
cin>>t;
while(t--)
{
ans=0;
cin>>n>>m;
init();
spfa(pre1,edge1);
spfa(pre2,edge2);
cout<<ans<<endl;
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值