HDU 6181 Two Paths

第一次写次短路

从起点和终点出发分别跑两次单源最短路得到dis1[] 和 dis2[],对所有边e的起点st,终点ed,将备选方案dis1[st]+dis2[ed]+e.length加入ans中,ans排序,选取次小的结果就是答案

没看数据范围错了两次,下次注意……

#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
vector<pair<LL,LL> > v[100100];
int vis[100100];
int st,ed,ind,test,n,m;
LL dis,len;
pair<LL,LL> tmp;
LL dis1[100100],dis2[100100];
priority_queue<pair<LL,LL>,vector<pair<LL,LL> >,greater<pair<LL,LL> > > q;
void dij(int st,LL *arr){
	while(!q.empty()) q.pop();
	memset(vis,0,sizeof(vis));
	for(int i = 1;i<=n;i++) arr[i] = 1e17;
	arr[st] = 0;
	q.push(make_pair(0,st));
	while(!q.empty()){
		tmp = q.top();
		q.pop();
		ind = tmp.second;
		if(vis[ind]) continue;
		vis[ind] = 1;
		arr[ind] = dis = tmp.first;
		for(int i = 0;i<v[ind].size();i++){
			int to = v[ind][i].second;
			if(!vis[to]){
				if(arr[to]>arr[ind]+v[ind][i].first) arr[to] = arr[ind]+v[ind][i].first,q.push(make_pair(arr[to],to));
			}
		}
	}
}
int main(){
	scanf("%d",&test);
	while(test--){
		scanf("%d%d",&n,&m);
		for(int i = 1;i<=n;i++) v[i].clear();
		for(int i = 0;i<m;i++){
			scanf("%d%d%lld",&st,&ed,&len);
			v[st].push_back(make_pair(len,ed));
			v[ed].push_back(make_pair(len,st));
		}
		dij(1,dis1);
		dij(n,dis2);
		//for(int i = 1;i<=n;i++) cout<<"dis1: "<<i<<" "<<dis1[i]<<endl;
		//for(int i = 1;i<=n;i++) cout<<"dis2: "<<i<<" "<<dis2[i]<<endl;
		vector<LL> ans;
		for(int i = 1;i<=n;i++){
			for(int j = 0;j<v[i].size();j++){
				int to = v[i][j].second;
				ans.push_back(v[i][j].first+dis1[i]+dis2[to]);
			}
		}
		sort(ans.begin(),ans.end());
		LL mmin = ans[0];
		for(int i = 0;i<ans.size();i++){
			if(ans[i] != mmin){
				cout<<ans[i]<<endl;
				break;
			}
		}
	}
	return 0;
}

  

时间: 2025-01-08 21:11:06

HDU 6181 Two Paths的相关文章

2017多校第10场 HDU 6181 Two Paths 次短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证明这题数据还是水了.下来我修改了一下次短路的,就可以避免这种情况了.提供一个sample 4 4 (1,2,1)( 1, 3,1) (2 4,1) (3 ,4,1).这组的正确答案是2,算法就来看代码吧. #include <bits/stdc++.h> using namespace std;

HDU 3191How Many Paths Are There(TOPE排序 求次短路及条数)

How Many Paths Are There Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 437 Problem Description oooccc1 is a Software Engineer who has to ride to the work place

HDU 6181 次短路(K短路)

Two Paths Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 613    Accepted Submission(s): 312 Problem Description You are given a undirected graph with n nodes (numbered from 1 to n) and m edge

hdu 1625 Numbering Paths 最短路的变形,使用Floyd 外加判环

Numbering Paths Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 158    Accepted Submission(s): 47 Problem Description Problems that process input and generate a simple ``yes'' or ``no'' answer

HDU 3191 How Many Paths Are There

How Many Paths Are There Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 319164-bit integer IO format: %I64d      Java class name: Main oooccc1 is a Software Engineer who has to ride to the work place every M

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include

HDU 4912 Paths on the tree

http://acm.hdu.edu.cn/showproblem.php?pid=4912 题意:给一棵树,再给一些路径,求最多有多少条路径不相交. 题解:主要是贪心的想法.用LCA处理出路径的层数,然后从最深处的节点往上找.因为节点越深,对其他路径影响度越小,相交的可能性越低.需要手动扩栈. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include

HDU 4912 Paths on the tree LCA 排序贪心

lca... 排个序然后暴力保平安 _(:зゝ∠)_ #pragma comment(linker, "/STACK:102400000,102400000") #include"cstdio" #include"iostream" #include"set" #include"queue" #include"string.h" using namespace std; #define

hdu 4912 Paths on the tree(树链拆分+贪婪)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LCA深度排序,从深度最大优先选.推断两个节点均没被标 记即为可选通道. 每次选完通道.将该通道LCA下面点所有标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #includ