链接:

http://poj.org/problem?id=1511

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#problem/J

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std; #define N 1000005
#define oo 0x3fffffff struct node
{
int a, b, next;
long long p;
}s1[N], s[N]; int n, m, head[N];
long long dis[N];
bool vis[N]; void Add(int a, int b, long long p, int k)
{
s1[k].a=a;
s1[k].b=b;
s1[k].p=p;
s1[k].next=head[a];
head[a]=k;
} long long spfa()
{
queue<int>Q;
Q.push();
vis[]=true; for(int i=; i<=n; i++)
dis[i]=oo;
dis[]=; while(Q.size())
{
int i = Q.front(); Q.pop();
vis[i] = false; for(int j=head[i]; j != ; j = s1[j].next)
{
int a = s1[j].a, b = s1[j].b;
int p = s1[j].p; if(dis[a]+p < dis[b])
{
dis[b] = dis[a] + p; if(vis[b] == false)
{
Q.push(b);
vis[b] = true;
}
}
}
} long long sum=; for(int i=; i<=n; i++)
sum += dis[i];
return sum;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m); int i; memset(head, , sizeof(head));
memset(s, , sizeof(s));
memset(s1, , sizeof(s1)); for(i=; i<=m; i++)
{
scanf("%d%d%I64d", &s[i].a, &s[i].b, &s[i].p);
Add(s[i].a, s[i].b, s[i].p, i);
} long long ans = spfa(); memset(head, , sizeof(head));
for(i=; i<=m; i++)
Add(s[i].b, s[i].a, s[i].p, i); ans += spfa();
printf("%I64d\n", ans);
}
return ;
}

(最短路 SPFA)Invitation Cards -- poj -- 1511的更多相关文章

  1. Invitation Cards POJ - 1511 (双向单源最短路)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  2. Invitation Cards POJ 1511 SPFA || dij + heap

    http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...

  3. Invitation Cards POJ - 1511

    题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...

  4. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

  5. (最短路 spfa)Wormholes -- poj -- 3259

    http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions ...

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

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

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

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

  8. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

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

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

随机推荐

  1. PHP框架(如:laravel、yii2、thinkPHP5)中统一异常处理及统一日志打印

    背景: 现在写接口服务应用有一个很通用的需求,想通过日志.或者监控的形式监测的接口的运行情况,比如耗时.请求参数.响应结果.和前端联调接口时或者排查线上问题时日志必不可少,特别是现场日志. 应用运行时 ...

  2. popup控件代码示例

    1.jsp页面input框中的代码 <td class="value"> <input name="demos[0].id" type=&qu ...

  3. 02 - Unit08:搜索笔记功能、搜索分页、处理插入数据库乱码问题

    搜索笔记功能 按键监听事件 $("#search_note").keydown(function(event){ var code=event.keyCode; if(code== ...

  4. python 简明教程

    第一个源程序 #!/usr/bin/python# Filename : helloworld.pyprint 'Hello world' 执行: $ python helloworld.py 或者  ...

  5. Gradle使用国内的maven仓库

    本文转载自:https://www.cnblogs.com/yoyotl/p/6291703.html 感谢阿里云! 找到gradle的配置文件路径,例如Windows中的路径为C:\Users\${ ...

  6. Spark在Windows下的环境搭建

    本文转载自:http://blog.csdn.net/u011513853/article/details/52865076 由于Spark是用Scala来写的,所以Spark对Scala肯定是原生态 ...

  7. Mysql8.0 Public Key Retrieval is not allow错误的解决办法

    在使用Mysql 8.0时重启后启动项目的事后会报错com.mysql.jdbc.exceptions.jdbc4.MysqlNonTransientConnectionException: Publ ...

  8. [原创][C#.Winform 控件]Krypton Suite comments

    命名空间:ComponentFactory.Krypton.Toolkit 最新版本:v4.4.0 官网状态:已停用 下载地址1:http://downloads.informer.com/krypt ...

  9. bat文件

    bat文件是dos下的批处理文件.批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cm ...

  10. canvas设置repeat

    canvas设置repeat 方法 ctx.createPattern(img, 'repeat'); repeat repeat-x repeat-y no-repeat 重复图片 const ca ...