B-旅行
题号:NC14550
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
小z放假了,准备到RRR城市旅行,其中这个城市有N个旅游景点。小z时间有限,只能在三个旅行景点进行游玩。小明租了辆车,司机很善良,说咱不计路程,只要你一次性缴费足够,我就带你走遍RRR城。
小z很开心,直接就把钱一次性缴足了。然而小z心机很重,他想选择的路程尽量长。
然而司机也很聪明,他每次从一个点走到另外一个点的时候都走最短路径。
你能帮帮小z吗?
需要保证这三个旅行景点一个作为起点,一个作为中转点一个作为终点。(一共三个景点,并且需要保证这三个景点不能重复).
输入描述:
本题包含多组输入,第一行输入一个整数t,表示测试数据的组数 每组测试数据第一行输入两个数N,M表示RRR城一共有的旅游景点的数量,以及RRR城中有的路的数量。 接下来M行,每行三个数,a,b,c表示从a景点和b景点之间有一条长为c的路 t<=40 3<=N,M<=1000 1<=a,b<=N 1<=c<=100
输出描述:
每组数据输出两行, 每组数据包含一行,输出一个数,表示整条路程的路长。 如果找不到可行解,输出-1.
示例1
输入
4 7 7 1 2 100 2 3 100 1 4 4 4 5 6 5 6 10 1 6 4 6 7 8 7 3 1 2 1 1 3 1 1 3 2 7 3 1 2 1 3 4 1 5 6 1 8 9 1 2 1 2 3 1 3 4 1 4 1 1 4 5 1 5 6 1 6 7 1 7 8 1 8 5 1
输出
422 3 -1 9
/*
*@Author: GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
// return a>b?a:b;
//}
//inline double min(double a,double b){
// return a<b?a:b;
//}
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
//void Fire(){
// queue<node> p;
// p.push({fx,fy,0});
// memset(fire, -1, sizeof(fire));
// fire[fx][fy]=0;
// while(!p.empty()){
// node temp=p.front();
// p.pop();
// for(int i=0;i<8;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
// continue;
// }
// fire[x][y]=temp.val+1;
// p.push({x,y,temp.val+1});
// }
// }
//}
//int bfs(){
// queue<node> p;
// memset(vis, 0, sizeof(vis));
// p.push({sx,sy,0});
// while (!p.empty()) {
// node temp=p.front();
// vis[temp.x][temp.y]=1;
// p.pop();
// for(int i=0;i<4;i++){
// int x=temp.x+xd[i];
// int y=temp.y+yd[i];
// if(x<0||x>=n||y<0||y>=m) continue;
// if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
// if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
// p.push({x,y,temp.val+1});
// }
// }
// return -1;
//}
//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
// ll ans=0;
// for(int i=0;i<s.size();i++){
// ans*=bas;
// ans+=int(s[i]);
// ans%=mod1;
// }
// return ans;
//}
//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
// p1[0]=1;
// p2[0]=1;
// for(int i=1;i<=505;i++){
// p1[i]=p1[i-1]*bas1;
// p2[i]=p2[i-1]*bas2;
//
// }
//}
//void Hash(){
// _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
// for(int i=1;i<=n;i++){ //前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
// }
// }
// for(int i=1;i<=n;i++){ //二维前缀和
// for(int j=1;j<=m;j++){
// _hash[i][j]+=_hash[i-1][j]*bas2;
// }
// }
//
//}
//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
// int value;
// node *l,*r;
// node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
// int flag=-1;
// for(int i=l;i<=r;i++){
// if(in[i]==pre[t]){
// flag=i;
// break;
// }
// }
// if(flag==-1) return;
// root=new node(in[flag]);
// t++;
// if(flag>l) builttree(l,flag-1,t,root->l);
// if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
// if(root!=NULL)
// {
// post[k++]=root->value;
// preorder(root->l);
// preorder(root->r);
//
// }
//}
//void inorder(node *root){
// if(root!=NULL)
// {
// inorder(root->l);
// post[k++]=root->value;
// inorder(root->r);
//
// }
//}
//void postorder(node *root){
// if(root!=NULL)
// {
// postorder(root->l);
// postorder(root->r);
// post[k++]=root->value;
// }
//}
//线段树
//#define MAX 1000000
//int tree[MAX<<2];
//void pushup(int rt){
// tree[rt]=tree[rt<<1]+tree[rt<<1|1];
//}
//void update(int rt,int i,int val,int l,int r)
//{
// if(r==l) {
// tree[rt]+=val;
// return;
// }
// if(i<=mid){
// update(ls,i,val,l,mid);
// }
// else {
// update(rs,i,val,mid+1,r);
// }
// pushup(rt);
//}
//int query(int rt,int l,int r,int k){
// if(l==r) return l;
// if(tree[rt<<1|1]>=k)
// return query(rs,mid+1,r,k);
// else return query(ls,l,mid,k-tree[rt<<1|1]);
//}
//二维树状数组
//int lowbit(int x){
// return x&(-x);
//}
//void updata(int x,int y,int c)
//{
// for(int i=x;i<=n;i+=lowbit(i)){
// for(int j=y;j<=n;j+=lowbit(j)){
// sum[i][j]+=c;
// }
// }
//}
//int getsum(int x,int y){
// int ans=0;
// for(int i=x;i>0;i-=lowbit(i)){
// for(int j=y;j>0;j-=lowbit(j)){
// ans+=sum[i][j];
// }
// }
// return ans;
//}
const int Maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
int head[Maxn], cnt, dis[Maxn], vis[Maxn], n, m;
queue<int> q;
struct node
{
int to, cost, next;
} w[maxn << 1];
void add(int a, int b, int c) //链式前向星存图
{
w[++cnt].to = b;
w[cnt].cost = c;
w[cnt].next = head[a];
head[a] = cnt;
}
int spfa(int x)
{
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[x] = 0; //标记起点
vis[x] = 1;
q.push(x);
while (!q.empty())
{
int v = q.front();
q.pop();
vis[v] = 0;
for (int i = head[v]; ~i; i = w[i].next) //依次遍历该节点的邻接点
{
node e = w[i];
if (dis[e.to] > e.cost + dis[v]) //松弛操作
{
dis[e.to] = e.cost + dis[v];
if (!vis[e.to]) //如果该结点没有被访问过就加入该节点
{
q.push(e.to);
vis[e.to] = 1; //标记这个点
}
}
}
}
int ma1 = -1, ma2 = -1;
dis[x] = -3;
for (int i = 1; i <= n; ++i) //遍历找出最大的和第二大的边——求和
{
if (dis[i] == inf)
continue;
if (dis[i] > ma1)
ma2 = ma1, ma1 = dis[i];
else if (dis[i] > ma2)
ma2 = dis[i];
}
if (ma1 != -1 && ma2 != -1)
return ma1 + ma2;
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
cnt = 0;
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c); //无向图加边两次
add(b, a, c);
}
int ans = -1;
for (int i = 1; i <= n; ++i)
ans = max(ans, spfa(i));
printf("%d\n", ans);
}
return 0;
}