Hamiltonian Path | ||
Accepted : 41 | Submit : 96 | |
Time Limit : 2000 MS | Memory Limit : 65536 KB |
原题链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1245
水题:有n个城市,问按顺序从1到n的最短路径。
AC代码:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <cstring>
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<a[y])
{
a[y]=z;
}
}
bool flag=true;
int ans=0;
for(int i=2;i<=n;i++)
{
if(a[i]<INF)
ans+=a[i];
else
{
flag=false;
break;
}
}
if(flag)
cout<<ans<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
尊重原创,转载请注明出处: http://blog.csdn.net/hurmishine