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 思路:思路不难只要多建一个逆序图,求出他的最短路加上之前正序图的最短路,就是答案,难点只要是数据太大容易爆容量和超时。所以选择链式前向星+spfa来实现。
实现代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define ll long long
#define M 1000100
#define INF 0x3f3f3f3f
int dist[M],vis[M],head[M],x[M],y[M],z[M],cnt,n;
struct edge{
int to,w,next;
}edge[M];
void add(int u,int v,int w){
edge[cnt].next = head[u];
edge[cnt].to = v;
edge[cnt].w = w;
head[u] = cnt++;
}
void spfa(int s){
memset(vis,,sizeof(vis));
for(int i = ;i <= n; i++) dist[i] = INF;
dist[s] = ;
queue<int>q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
vis[u] = ;
for(int k = head[u]; k ;k = edge[k].next){
int v = edge[k].to;
if(dist[v] > dist[u] + edge[k].w){
dist[v] = dist[u] + edge[k].w;
if(!vis[v]){
vis[v] = ;
q.push(v);
}
}
}
}
} int main(){
int t,m;
while(scanf("%d",&t)!=EOF){
while(t--){
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
scanf("%d%d",&n,&m);
cnt = ;
for(int i = ;i <= m; i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
add(x[i],y[i],z[i]);
}
spfa();
ll ans = ;
for(int i = ;i <= n; i++){
ans += dist[i];
//cout<<dist[i]<<endl;
}
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
cnt = ;
for(int i = ;i <= m; i++){
add(y[i],x[i],z[i]);
}
spfa();
for(int i = ;i <= n; i++){
ans += dist[i];
//cout<<dist[i]<<endl;
}
cout<<ans<<endl;
}
}
}

poj 1511 Invitation Cards(最短路中等题)的更多相关文章

  1. POJ - 1511 Invitation Cards(Dijkstra变形题)

    题意: 给定一个有向图,求从源点到其他各点的往返最短路径和.且这个图有一个性质:任何一个环都会经过源点. 图中的节点个数范围:0-100w; 分析: 我们先可以利用Dijkstra算法求解从源点到其余 ...

  2. 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 / ...

  3. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  4. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  5. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

  6. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  7. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  9. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

随机推荐

  1. C# 自定义类型转换

    1.显式转换和隐式转换: ; long b=a; // 从int到long的隐式转换 int c=(int) b; // 从long到int是显式转换 ------------------------ ...

  2. less初识

    一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, Firefox),也可以 ...

  3. TortoiseSVN 只取下或更新部分文件的方法(Sparse Update/Sparse Checkout)

    Sparse Update/Sparse Checkout   To easily select only the items you want for the checkout and force ...

  4. Codeforces round 1106

    Div 2 536 题目链接 我还是太菜了.jpg E 傻逼DP直接做 我居然调了1.5h 我真的是太菜了.jpg 堆+扫描线直接维护每个位置的贪心结果 然后要么使用干扰 要么就接受贪心的结果 #in ...

  5. 避免写慢SQL

    最近在整理数据库中的慢SQL,同时也查询了相关资料.记录一下,要学会使用执行计划来分析SQL. 1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且 ...

  6. jQuery调用Asp.Net后台方法

    常用的ajax就不讲了,这里主要是说通过ajax调用asp.net后台的cs文件暴露的方法. 前台: <%@ Page Language="C#" AutoEventWire ...

  7. GIT命令基本使用

    记录摘选自廖雪峰的官方网站归纳总结 1.centos下安装git [root@cdw-lj ~]# yum install git 2.配置用户名以及邮箱 [root@cdw-lj opt]# git ...

  8. P2371 [国家集训队]墨墨的等式

    膜意义下最短路. 把最小的\(a\)抠出来,作为模数\(mod\),然后建点编号为\(0\)到\(mod-1\),对每个数\(a\)连边\((i,(a+i)\mod mod)\)点\(i\)的最短路就 ...

  9. stl源码剖析 详细学习笔记stack queue

    // //  stack.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/1 ...

  10. Visual Studio控制台程序输出窗口一闪而过的解决方法

    转载大牛的博客,自己也遇到了类似的问题,解决方法很详细,也很管用   刚接触 Visual Studio的时候大多数人会写个Hello World的程序试一下,有的人会发现执行结束后输出窗口会一闪而过 ...