输入样例:
6 13
0 5 2
6 2 2
6 0 1
3 4 2
1 5 2
2 5 1
3 1 1
4 1 2
1 6 1
6 3 2
1 2 1
4 5 3
2 0 2
7
6 5 1 4 3 6 2
6 5 2 1 6 3 4
8 6 2 1 6 3 4 5 2
3 2 1 5
6 6 1 3 4 5 2
7 6 2 1 3 4 5 2
6 5 2 1 4 3 6
输出样例:
3
5 11
答案
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
const int N = 211;
using namespace std;
int n,m;
int mp[N][N];
int dp[N];
int dis[N];
int main(){
cin>>n>>m;
while(m--){
int u,v,w;
cin>>u>>v>>w;
mp[u][v]=mp[v][u]=w;
}
int q;
cin>>q;
int minn=INT_MAX;
int id;
int tot=0;
for(int qq=1;qq<=q;qq++){
int k;
cin>>k;
set<int>st;
for(int i=1;i<=k;i++){
cin>>dp[i];
st.insert(dp[i]);
}
if(!mp[0][dp[1]]||!mp[0][dp[k]]) continue;
if((int)st.size()<k) continue;
if(k!=n) continue;
int s=0;
int sum=0;
bool book=0;
for(int i=1;i<=k;i++){
if(mp[s][dp[i]]){
sum+=mp[s][dp[i]];
// cout<<s<<" "<<dp[i]<<" "<<mp[s][dp[i]]<<endl;
s=dp[i];
}
else{
book=1;
break;
}
}
if(book) continue;
sum+=mp[0][dp[k]];
tot++;
// cout<<qq<<" "<<endl;
if(minn>sum){
minn=sum;
id=qq;
}
}
cout<<tot<<endl;
cout<<id<<" "<<minn<<endl;
return 0;
}