J - Invitation Cards 最短路
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 数据量太大必须用堆优化。。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define MAXN 1000010
#define INF 0x3f3f3f3f
typedef long long LL;
/*
两次 Dijkstra
结点数目太多 必须优化
*/
struct qnode
{
LL v,c;
qnode(LL _v=,LL _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
LL v,cost;
Edge(LL _v=,LL _cost=):v(_v),cost(_cost){}
};
struct node
{
LL f,t,d;
};
vector<Edge> E[MAXN];
node tmp[MAXN];
bool vis[MAXN];
LL dist[MAXN],n,m;
void Dijkstra(LL n,LL start)
{
memset(vis,false,sizeof(vis));
for(LL i=;i<=n;i++)
dist[i] = INF;
dist[start] = ;
priority_queue<qnode> q;
while(!q.empty()) q.pop();
q.push(qnode(start,));
qnode tmp;
while(!q.empty())
{
tmp = q.top();
q.pop();
LL u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(LL i=;i<E[u].size();i++)
{
LL v = E[u][i].v;
LL cost = E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v] = dist[u]+cost;
q.push(qnode(v,dist[v]));
}
}
}
}
void Add(LL u,LL v,LL cost)
{
E[u].push_back(Edge(v,cost));
} int main()
{
LL t;
cin>>t;
while(t--)
{
scanf("%lld%lld",&n,&m);
LL ans=;
for(LL i=;i<=n;i++) E[i].clear();
for(LL i=;i<m;i++)
{
scanf("%lld%lld%lld",&tmp[i].f,&tmp[i].t,&tmp[i].d);
Add(tmp[i].f,tmp[i].t,tmp[i].d);
}
Dijkstra(n,);
for(LL i=;i<=n;i++)
{
ans+=dist[i];
E[i].clear();
}
for(LL i=;i<m;i++)
{
Add(tmp[i].t,tmp[i].f,tmp[i].d);
}
Dijkstra(n,);
for(LL i=;i<=n;i++)
{
ans+=dist[i];
}
printf("%lld\n",ans);
}
return ;
}
J - Invitation Cards 最短路的更多相关文章
- D - Silver Cow Party J - Invitation Cards 最短路
http://poj.org/problem?id=3268 题目思路: 直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路. 还有一种方法会快很多, ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- POJ1511 Invitation Cards —— 最短路spfa
题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Tota ...
- POJ-1511 Invitation Cards( 最短路,spfa )
题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...
- hdu1535 Invitation Cards 最短路
有一张图,若干人要从不同的点到同一个中间点,再返回,求总费用最小 中间点到各个点最小费用是普通的最短路 各个点到中间点最小费用其实就是将所有路径反向建边之后中间点到各个点的最小费用,同样用最短路就可以 ...
- J - Invitation Cards
题目大意:邀请卡 在电视的时代,没有多少人会去剧院观看演出.古老的喜剧演员 Malidinesia知道这个事实.他们想传播戏剧尤其是古老的戏剧,他们在邀请卡上打印必要的信息和一些节目,一些学生被雇佣过 ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- Invitation Cards POJ - 1511 (双向单源最短路)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
随机推荐
- tp5增加验证的自定义规则
- 洛谷 P1414 又是毕业季II(未完成)
题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...
- 【js】再谈移动端的模态框实现
移动端模态框的机制因为与PC的模态框机制一直有所区别,一直是许多新人很容易踩坑的地方,最近笔者作为一条老咸鱼也踩进了一个新坑中,真是平日里代码读得太粗略,故而写上几笔,以儆效尤. 故事的起因是这样的, ...
- ACM_下一个排列
The Next Permutation Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- Spring加载applicationContext.xml实现spring容器管理的几种方式
package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...
- WCF wsdlexception(at/html):faultCode=INVALID_WSDL
WCF 部署正常,通过浏览器查看服务也OK,但是通过SOAP UI创建客户端请求时就异常: wsdlexception(at/html):faultCode=INVALID_WSDL: Expecte ...
- 关于mybatis的xml文件中使用 >= 或者 <= 号报错的解决方案
当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...
- hanframe开微博了
之前一直在百度里转一些文章,平时都把积累的东西放在一些文档中,还是想着记下来会比较好一点,顺便,每天都来这里做一点总结吧
- 使用QTP录制自带Flight小实例
1.双击打开QTP10.0,启动过程中测试类型选择“WEB”. 2.进入主界面,New——Test,新建一个测试用例. 3.点击Record按钮,Record and settings对话框中,可以选 ...
- PHP实现写LOG日志的代码
这篇文章给大家介绍的内容是关于PHP实现写LOG日志的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. public function write_log(){ //设置目录时间 ...