题目链接:https://vjudge.net/contest/123674#problem/D

题意:一张个向图,求从点1开始到其他各点的最短路权值和加上从其他各点到点1的最短路权值和 首先注意的是这是一个有向图,既要求1到所有的点的距离又要求其他所有点到1的距离,由于只学过spfa算法,就又用了spfa算法,求1到其他点的距离比较好求,但其他点到1的距离就不太好求了,我用到了图的转置,就是把所有的边的方向都反向,给建立一个新的图,这样1以外的点到1的距离就是1到1以外的点的距离;

用习惯了Java还真不习惯c++,特别是看到指针就烦,但Java这时间也真吓人的,

AC代码: time: 21282ms  好可怕!

 import java.util.*;
import java.io.*; public class Main {
public static Ver[] array1;
public static Ver[] array2;
public static int[] head1;
public static int[] head2;
public static int n;
public static long[] dis1;
public static long[] dis2;
public static boolean[] judge;
public static long Max = ;
public static long[] result;
public static int count; /**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner s = new Scanner(new BufferedInputStream(System.in));
int t = s.nextInt(); while (t-- > ) { n = s.nextInt();
int m = s.nextInt();
array1 = new Ver[];
array2 = new Ver[];
head1 = new int[n + ];
head2 = new int[n + ];
count = ;
for (int i = ; i < m; i++) { //建图
int a = s.nextInt();
int b = s.nextInt();
long c = s.nextLong();
array1[count] = new Ver(b, c); //正图
array1[count].next = head1[a];
head1[a] = count; array2[count] = new Ver(a, c); //反图
array2[count].next = head2[b];
head2[b] = count;
count++;
}
result = new long[n + ];
spfa1();
spfa2();
long ans = ;
for (int i = ; i <= n; i++) { //求和
ans += dis1[i] + dis2[i];
}
System.out.println(ans);
}
s.close();
} public static void spfa2(int a) { //反向图的spfa算法
dis2 = new long[n + ];
judge = new boolean[n + ];
for (int i = ; i < n + ; i++) {
dis2[i] = Max; }
LinkedList<Integer> queue = new LinkedList<Integer>();
judge[] = true;
dis2[] = ;
queue.add();
while (!queue.isEmpty()) {
int vpoll = queue.poll();
judge[vpoll] = false; int temp = head2[vpoll];
while (temp != ) {
if (relax2(vpoll, array2[temp].value, array2[temp].distance)) {
if (!judge[array2[temp].value]) {
judge[array2[temp].value] = true;
queue.add(array2[temp].value);
}
}
temp = array2[temp].next;
}
} } public static void spfa1(int a) { //正向图的spfa算法
dis1 = new long[n + ];
judge = new boolean[n + ];
for (int i = ; i < n + ; i++) {
dis1[i] = Max; }
LinkedList<Integer> queue1 = new LinkedList<Integer>();
judge[a] = true;
dis1[a] = ;
queue1.add(a);
while (!queue1.isEmpty()) {
int vpoll = queue1.poll();
judge[vpoll] = false; int temp = head1[vpoll];
while (temp != ) {
if (relax1(vpoll, array1[temp].value, array1[temp].distance)) {
if (!judge[array1[temp].value]) {
judge[array1[temp].value] = true;
queue1.add(array1[temp].value);
}
}
temp = array1[temp].next;
}
} } public static boolean relax1(int a, int b, long c) { if (dis1[a] + c < dis1[b]) {
dis1[b] = dis1[a] + c;
return true;
}
return false;
} public static boolean relax2(int a, int b, long c) { if (dis2[a] + c < dis2[b]) {
dis2[b] = dis2[a] + c;
return true;
}
return false;
} } class Ver { //图类
long distance;
int value;
int next; Ver(int value, long distance) {
this.value = value;
this.distance = distance;
}
}

2016huasacm暑假集训训练三 D - Invitation Cards的更多相关文章

  1. 2016huasacm暑假集训训练三 G - 还是畅通工程

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/G 这题和上一道题差不多,还更简单点,直接用prim算法就行,直接贴AC代码: im ...

  2. 2016huasacm暑假集训训练三 F - Jungle Roads

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/F 题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使 ...

  3. 2016huasacm暑假集训训练三 C - Til the Cows Come Home

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/C N题目大意是有n个点,然后给出从a点到b点的距离,a和b是互相可以抵达的,则是无 ...

  4. 2016huasacm暑假集训训练三 B-Frogger

    题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/B 题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏 ...

  5. 2016huasacm暑假集训训练五 H - Coins

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...

  6. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  7. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  8. 2016huasacm暑假集训训练五 F - Monkey Banana Problem

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...

  9. 2016huasacm暑假集训训练五 E - What Is Your Grade?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/E 题意:给做出的题目个数,5个的100分,4个的前n/2的同学95,后n/2的90 ...

随机推荐

  1. spring常见问题

    问题1:提示说:cvc-elt.1: Cannot find the declaration of element 'beans' 解决方法:从网上搜了一些,有的说是因为网络原因访问不到xsd文件,因 ...

  2. php gettext 多语言翻译

    1.在window与linux下的多语言切换有些区别,主要putenv的设置区别. 参考链接:http://www.cnblogs.com/sink_cup/archive/2013/11/20/ub ...

  3. cookie中文乱码

    在学习当中碰到cookie中文乱码问题,问题原因:cookie对中文不太支持,将中文放入cookie中会报错误. 解决办法: 1.编码 将中文进行编码再放入cookie中: String userna ...

  4. php 操作数组 (合并,拆分,追加,查找,删除等)

    1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...

  5. 记录一下两个比较常用的md5加密算法

    第一个,计算字符串的md5值 public static String getMD5(String s){ String newString = null; byte[] inputByteArray ...

  6. BZOJ 1833: [ZJOI2010]count 数字计数

    Description 问 \([L,R]\) 中0-9的个数. Sol 数位DP. 预处理好长度为 \(i\), 最高位为 \(j\) 的数位之和. 然后从上往下计算,不要忘记往下走的同时要把高位的 ...

  7. 【转载总结】jQuery和HTML5全屏焦点图

    选项设置与说明 Slider Revolution提供了很多参数选项设置: delay: 滑动内容停留时间.默认9000毫秒 startheight: 滑动内容高度,默认490像素. startwid ...

  8. CentOS7 屏幕亮度的命令行管理

    1.安装:yum install xgamma 设置亮度:xgamma -gamma n( 0.1 < n < 10.0 ,可以根据自己的喜好设置 2.shell 系统调节亮度调用的是/s ...

  9. 基于Struts2CRUD的质量属性

    基于struts2框架开发的<学生管理系统>的质量属性 我们经常重新设计系统,可能不是因为该系统在功能上有缺陷,而是由于:系统运行速度太慢.系统容易受到外界攻击.用另外的一句话说:我们修改 ...

  10. 使用FTP FtpWebRequest UsePassive 属性实现主动上传

    类型:System::Boolean如果客户端应用程序的数据传输过程侦听数据端口上的连接,则为 false:如果客户端应在数据端口上启动连接,则为 true. 默认值为 true. UsePassiv ...