您的位置:首页 > 其它

HDU 1535 SPFA 前向星存图优化

2016-03-05 12:20 357 查看

Invitation Cards

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

46
210

[align=left]Source[/align]
Central Europe 1998

题意: 一个单向图 求1到给点的最短距离 和各个点到1的最短距离之和
题解:正向反向 两次建图 前向星存图 前向星 传送门 http://blog.csdn.net/acdreamers/article/details/16902023 struct node

{
int pr;//同起点的上一条边
int to;//单向边的终点
int w;//边的权值
} edge1[MAXN],edge2[MAXN];// 存边


加边操作

void add1(int a,int b,int c) //a-->b
{
nedge1++;//初始为零
edge1[nedge1].to=b;
edge1[nedge1].w=c;
edge1[nedge1].pr=pre1[a];
pre1[a]=nedge1;//以a为起点的下一条边的位置(位置其实就是边的序号) pre1数组初始为0;

//(发现这里是逆向的 一层一层的往上找 指导 pre1数组的值为0 ) 遍历的时候
}

SPFA 姿势

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define MAXN 1000001
#define inf 100000000
using namespace  std;
struct node
{
int pr;
int to;
int w;
}edge1[MAXN],edge2[MAXN];
int pre1[MAXN],pre2[MAXN];
int dis1[MAXN],dis2[MAXN];
int nedge1=0;
int nedge2=0;
int n;
int p,q;
int be,ed,we;
queue<int >qq;
int now;
int vis[MAXN];
void add1(int a,int b,int c)
{
nedge1++;
edge1[nedge1].to=b;
edge1[nedge1].w=c;
edge1[nedge1].pr=pre1[a];
pre1[a]=nedge1;
}
void add2(int a,int b,int c)
{
nedge2++;
edge2[nedge2].to=b;
edge2[nedge2].w=c;
edge2[nedge2].pr=pre2[a];
pre2[a]=nedge2;
}
void spfa1()
{
for(int gg=1;gg<=p;gg++)
{
vis[gg]=0;
dis1[gg]=inf;
}
vis[1]=1;
dis1[1]=0;
qq.push(1);
while(!qq.empty())
{
now=qq.front();
vis[now]=0;
qq.pop();
for(int k=pre1[now];k!=0;k=edge1[k].pr)
{
int mm=edge1[k].to;
if(dis1[now]+edge1[k].w<dis1[mm])
{
dis1[mm]=dis1[now]+edge1[k].w;
if(!vis[mm])
{
vis[mm]=1;
qq.push(mm);
}
}
}
}
}
void spfa2()
{
for(int gg=1;gg<=p;gg++)
{
vis[gg]=0;
dis2[gg]=inf;
}
vis[1]=1;
dis2[1]=0;
qq.push(1);
while(!qq.empty())
{
now=qq.front();
qq.pop();
vis[now]=0;
for(int k=pre2[now];k!=0;k=edge2[k].pr)
{
int mm=edge2[k].to;
if(dis2[now]+edge2[k].w<dis2[mm])
{
dis2[mm]=dis2[now]+edge2[k].w;
if(!vis[mm])
{
vis[mm]=1;
qq.push(mm);
}
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&p,&q);
nedge1=0;nedge2=0;
for(int gg=1;gg<=p;gg++)
{
pre1[gg]=0;
pre2[gg]=0;
}
for(int j=1;j<=q;j++)
{
scanf("%d %d %d",&be,&ed,&we);
add1(be,ed,we);
add2(ed,be,we);
}
spfa1();
spfa2();
int sum=0;
for(int e=1;e<=p;e++)
sum=sum+dis1[e]+dis2[e];
printf("%d\n",sum);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: