您的位置:首页 > 其它

POJ 1511 Invitation Cards(优先队列优化后的迪杰斯特拉)

2018-01-19 13:48 274 查看
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

题目大意:n个站点,m条路线,每个人从第一个站点到其他n-1个站点去,并回来,求来回的最小花费。因为数据量庞大,而且需要求两次最短路径,所以选择用优先队列优化的迪杰斯特拉。先将路线正着存图找一遍最小花费,然后再反向存图找一遍最小花费,将两次的花费加起来就是了。只不过我一开始写的是向迪杰斯特拉函数传了一个vector数组,然后一直超时,后面改过来就好了。不知道为什么,难道是传参(vector数组)费时???
//超时,why???
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
using namespace std;
#define inf 0x7fffffff
#define LL long long
const int N=1000010;
int m,n;
LL dis
;
bool vis
;
struct node
{
int v;
int dis;
node(int a,int b) : v(a),dis(b) {}
bool operator <(const node &x)const
{
return dis>x.dis;
}
};
vector<node> adj1
,adj2
;//存图,两个vector数组,分别存正反两张图
priority_queue<node> q;
void dijkstra(vector<node> adj[])//传参
{
fill(dis,dis+N,inf);
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop();
q.push(node(0,0));
dis[0]=0;
while(!q.empty())
{
int u=q.top().v;
q.pop();
if(vis[u]) continue;
vis[u]=true;
for(int j=0;j<adj[u].size();j++)
{
int v=adj[u][j].v;
int diss=adj[u][j].dis;
if(!vis[v]&&diss+dis[u]<dis[v])
{
dis[v]=dis[u]+diss;
q.push(node(v,dis[v]));
}
}
}
}
int main()
{
LL ans;
int t,u,v,wt;
scanf("%d",&t);
while(t--)
{
ans=0;
for(int i=0;i<=n;i++)
{
adj1[i].clear();//清零
adj2[i].clear();
}
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&wt);
adj1[u-1].push_back(node(v-1,wt));//正向图
adj2[v-1].push_back(node(u-1,wt));//反向图
}
dijkstra(adj1);//正向图跑一遍将所有花费加起来
for(int i=1;i<n;i++)
ans+=dis[i];
dijkstra(adj2);//反向图跑一遍将所有花费加起来
for(int i=1;i<n;i++)
ans+=dis[i];
printf("%lld\n",ans);
}
}
//AC代码,跟上面的代码真的只是传了一个vector数组啊,为什么上面的就不可以呢???
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
using namespace std;
#define inf 0x7fffffff
#define LL long long
const int N=1000010;
int m,n,u
,v
,wt
;
LL dis
;
bool vis
;
struct node
{
int v;
int dis;
node(int a,int b) : v(a),dis(b) {}
friend bool operator < (node x,node y)
{
return x.dis>y.dis;
}
};
vector<node> adj
;
void dijkstra()
{
fill(dis,dis+n,inf);
memset(vis,false,sizeof(vis));
priority_queue<node> q;
while(!q.empty()) q.pop();
dis[0]=0;
q.push(node(0,0));
while(!q.empty())
{
int u=q.top().v;
q.pop();
if(vis[u]) continue;
vis[u]=true;
for(int j=0;j<adj[u].size();j++)
{
int v=adj[u][j].v;
int diss=adj[u][j].dis;
if(!vis[v]&&diss+dis[u]<dis[v])
{
dis[v]=dis[u]+diss;
q.push(node(v,dis[v]));
}
}
}
}
int main()
{
LL ans;
int t;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d%d",&u[i],&v[i],&wt[i]);
for(int i=0;i<n;i++)
adj[i].clear();
for(int i=0;i<m;i++)
adj[u[i]-1].push_back(node(v[i]-1,wt[i]));
dijkstra();
for(int i=0;i<n;i++)
ans+=dis[i];
for(int i=0;i<n;i++)
adj[i].clear();
for(int i=0;i<m;i++)
adj[v[i]-1].push_back(node(u[i]-1,wt[i]));
dijkstra();
for(int i=0;i<n;i++)
ans+=dis[i];
printf("%lld\n",ans);
}
}
若有大牛知道我第一段代码超时的原因请不吝赐教,小弟不胜感激


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