$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$C$题

简单题。

注意题目中给出的数据范围:$1 \le ai < bi \le n$,说明这是一个有向无环图,并且哈密顿路一定是$1 \to 2 \to 3 \to  \cdots  \cdots  \to n$。

因此这题就很简单了。哎,明明知道这原本是一个$NP$问题,但在比赛的时候还在不停的想怎么做,没有考虑到数据给出了这样的范围.....真是菜。最后还是机智的队友发现了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-; const int maxn=;
int n,m;
LL a[maxn]; int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(a,-,sizeof a);
for(int i=;i<=m;i++)
{
int u,v; LL w; scanf("%d%d%lld",&u,&v,&w);
if(u!=v-) continue;
if(a[v]==-) a[v]=w;
else a[v]=min(a[v],w);
}
bool fail=;
for(int i=;i<=n;i++) if(a[i]==-) fail=;
if(fail) printf("-1\n");
else
{
LL sum=;
for(int i=;i<=n;i++) sum=sum+a[i];
printf("%d\n",sum);
}
}
return ;
}

XTU 1245 Hamiltonian Path的更多相关文章

  1. 湘潭邀请赛 Hamiltonian Path

    湘潭邀请赛的C题,哈密顿路径,边为有向且给定的所有边起点小于终点,怎么感觉是脑筋急转弯? 以后一定要牢记思维活跃一点,把复杂的事情尽量简单化而不是简单的事情复杂化. #include<cstdi ...

  2. Android使用静默安装时碰见的问题

    升级时碰见的异常 private void installPackage(String appName,final File apk) { if (!apk.exists()) { setHasNew ...

  3. 北京地铁站点遍历最少经站次数问题普遍意义上是一个NP问题,目前不存在多项式时间算法能够解决该问题

    http://www.cnblogs.com/jiel/p/5852591.html 众所周知求一个图的哈密顿回路是一个NPC问题: In the mathematical field of grap ...

  4. codeforces 577E E. Points on Plane(构造+分块)

    题目链接: E. Points on Plane time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #319 (Div. 1) C. Points on Plane 分块

    C. Points on Plane Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/pro ...

  6. hdu 5424 Rikka with Graph II(dfs+哈密顿路径)

    Problem Description   As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...

  7. Graph图总结

    将COMP20003中关于Graph的内容进行总结,内容来自COMP20003,中文术语并不准确,以英文为准. Graph G = {V, E} 顶Vertices V: can contain in ...

  8. List of NP-complete problems

    This is a list of some of the more commonly known problems that are NP-complete when expressed as de ...

  9. NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)

    NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...

随机推荐

  1. vector cin

    vector<int>vec1,vec2; int ival; cout<<"Ender numbers for vector1(-1 to end):"& ...

  2. Tomcat7 Cluster 集群

    Tomcat7 自带的集群功能是通过session复制完成的,现有两个复制方式: DeltaManager: 将session复制到所有tomcat节点中,不管是否有相应的应用(it will rep ...

  3. System.Net.Http 简介

    System.Net.Http 简介 System.Net.Http 是微软推出的最新的 HTTP 应用程序的编程接口, 微软称之为“现代化的 HTTP 编程接口”, 主要提供如下内容: 1. 用户通 ...

  4. django 创建一个通用视图

    创建一个通用视图   抽取出我们代码中共性的东西是一个很好的编程习惯. 比如,像以下的两个Python函数:     def say_hello(person_name): print 'Hello, ...

  5. 通过实体反射实现CriteriaQuery并列条件查询

    将实体反射之后获取查询字段的值,并添加到Predicate对象数组中 public Predicate getPredicateAnd(T entity, Root<T> root, Cr ...

  6. ios学习笔记(一)之Object-C

    一:Objective-C语言基础 二:Objective-C类与继承和协议 一:Objective-C语言基础: 1.1)#import 包含头文件  与#include 作用相同 1.2)Obje ...

  7. python-igraph on windows10 64bit

    igraph安装记录: 在http://www.lfd.uci.edu/~gohlke/pythonlibs/找到对应版本的python-igraph 这里是anaconda的python2.7.11 ...

  8. Arduino 3G shield using SoftwareSerial to control

    On the 3G shield, by default the power pin is on D8 and reset pin is on D9. Make it HIGH then it wor ...

  9. MFC 数据库编程 增删改查的一个例子

    1.先看下效果图: 主界面: 一个最简单的1对多的表,一张Article(文章)表,一张Category(类别)表.有添加,修改,删除文章按钮.点击类别编辑按钮就会跳到第二个对话框.点击数据库配置就会 ...

  10. 在 Cordova/Phonegap for Android 中包含中文文件名的页面

    在 Cordova/Phonegap for Android 中包含中文文件名的页面 本贴首发于: http://xuekaiyuan.com/forum.php?mod=viewthread& ...