POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 16178 | Accepted: 5262 |
Description
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
Output
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
//============================================================================
// Name : POJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
/*
* 使用优先队列优化Dijkstra算法
* 复杂度O(ElogE)
* 注意对vector<Edge>E[MAXN]进行初始化后加边
*/
const int INF=0x3f3f3f3f;
const int MAXN=;
struct qnode
{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n,int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=;i<E[u].size();i++)
{
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
}
int A[MAXN],B[MAXN],C[MAXN];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
scanf("%d%d%d",&A[i],&B[i],&C[i]);
for(int i=;i<=n;i++)E[i].clear();
for(int i=;i<m;i++)addedge(A[i],B[i],C[i]);
Dijkstra(n,);
long long ans=;
for(int i=;i<=n;i++)ans+=dist[i];
for(int i=;i<=n;i++)E[i].clear();
for(int i=;i<m;i++)addedge(B[i],A[i],C[i]);
Dijkstra(n,);
for(int i=;i<=n;i++)ans+=dist[i];
printf("%I64d\n",ans);
}
return ;
}
POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)的更多相关文章
- Invitation Cards POJ - 1511 (双向单源最短路)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- poj 1511 Invitation Cards (最短路)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 33435 Accepted: 111 ...
- POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
随机推荐
- java连接Oracle数据库实现增删改查并在Navicat中显示
创建TEST表 eclipse中的java项目 代码 数据库方法类 DBUtil: package util; import java.sql.Connection; import java.sql. ...
- C# 命名管道
有些场合需要高效率,进行线程间通信,可以使用 C#命名管道.
- 最小生成树 $Kruskal$ 算法
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; int h[maxn], v[maxn], ...
- 关于usr/bin/ld: cannot find -lxxx问题总结(Qt编译错误cannot find -lGL)
决定终结这个问题!(网上要想找到完整的解答实在太难了) http://blog.sciencenet.cn/blog-676535-541444.html 前两天手贱,把虚拟机玩崩溃了,只好重装虚拟机 ...
- KVC 开发详情
目录 概述 KVC基本技术 KVC访问函数 KVC搜索顺序 KVC集合操作 一.概述 kvc全名是Key-value coding,kvc是一种通过字符串间接的访问oc对象的属性的一种技术. 一个oc ...
- Java设计模式中适配器模式的实现方法
在Java开发中,我们常常需要用到Java接口型模式中的适配器模式,那适配器设计模式到底是什么模式呢? 适配器模式(Adapter)就是把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹 ...
- P3153 [CQOI2009]跳舞
题目描述 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会”单向喜欢“) ...
- 2016年NK冬季训练赛 民间题解
A题 水题,考察对行的读入和处理,注意使用long long #include <iostream> #include <cstring> #include <cstdi ...
- A Dangerous Maze (II) LightOJ - 1395(概率dp)
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
- 【POJ 2406 Power Strings】
Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...