2016湘潭邀请赛 xtu1245

Hamiltonian Path

In ICPCCamp, there are n cities and m directed roads between cities. The i-th road going from the ai-th city to the bi-th city is ci kilometers long. For each pair of cities (u,v), there can be more than one roads from u to v.

Bobo wants to make big news by solving the famous Hamiltonian Path problem. That is, he would like to visit all the n cities one by one so that the total distance travelled is minimized.

Formally, Bobo likes to find n distinct integers p1,p2,,pn to minimizew(p1,p2)+w(p2,p3)+...+w(pn−1,pn)where w(x,y) is the length of road from the x-th city to the y-th city.

Input

The input contains at most 30 sets. For each set:

The first line contains 2 integers n,m (2n105,0m105).

The i-th of the following m lines contains 3 integers ai,bi,ci (1ai<bin,1ci104).

Output

For each set, an integer denotes the minimum total distance. If there exists no plan, output -1 instead.

Sample Input

3 3
1 2 1
1 3 1
2 3 1
3 2
1 2 1
1 3 2

Sample Output

2
-1


题意:

见题目中标记的红色部分,提示说是从1 到 2,2  到3 的路径

OMG的,题目如此之水,就是需要认真审题


#include
#include
#include
using namespace std;

#define INF 0x3f3f3f
#define MANX 100010
int arr[MANX];

int main()
{
    int a,b,c;
    int n,m;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(arr,INF,sizeof(arr));
        for(int i=0;i


你可能感兴趣的:(acm)