XTOJ 1245 Hamiltonian Path【伪最短路,水题】

Hamiltonian Path

Accepted : 41   Submit : 96
Time Limit : 2000 MS   Memory Limit : 65536 KB

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 minimize w(p1,p2)+w(p2,p3)++w(pn1,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

原题链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1245

水题:有n个城市,问按顺序从1到n的最短路径。

AC代码:

/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  *
*/

#include 
#include 
#include 
using namespace std;
const int maxn=1e5;
const int INF=1e9;
int a[maxn];
int main()
{
    //freopen("data.txt","r",stdin);
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=2;i<=n;i++)
            a[i]=INF;
        int x,y,z;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(y-x==1&&z

尊重原创,转载请注明出处: http://blog.csdn.net/hurmishine


你可能感兴趣的:(Other,OJ,ACM_基础题(水题),ACM_HDU刷题录,XTOJ,1245,Hamiltonian,Path,伪最短路,水题)