算出从点1到点n的最短路径。

/* ***********************************************
Author :guanjun
Created Time :2016/6/23 22:58:19
File Name :1019.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int dis[][];
int n,m;
int lowcost[];
int vis[];
int dij(){
cle(vis);
vis[]=;
memset(lowcost,INF,sizeof lowcost);
for(int i=;i<=n;i++)lowcost[i]=dis[][i];
lowcost[]=;
int k,Min;
for(int i=;i<=n;i++){
Min=INF;
for(int j=;j<=n;j++){
if(lowcost[j]<Min&&!vis[j])Min=lowcost[j],k=j;
}
if(Min==INF)break;
vis[k]=;
for(int j=;j<=n;j++){
if(lowcost[j]>lowcost[k]+dis[k][j]&&!vis[j])
lowcost[j]=lowcost[k]+dis[k][j];
}
}
return lowcost[n];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,x,y;
ll z;
cin>>T;
for(int t=;t<=T;t++){
cin>>n>>m;
memset(dis,INF,sizeof dis);
for(int i=;i<=m;i++){
cin>>x>>y>>z;
if(dis[x][y]>z)dis[y][x]=dis[x][y]=z;
}
int ans=dij();
if(ans==INF){
printf("Case %d: Impossible\n",t);
}
else printf("Case %d: %d\n",t,ans); }
return ;
}

Lightoj 1019 - Brush (V)的更多相关文章

  1. Light OJ 1019 - Brush (V)(图论-dijkstra)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1019 题目大意:Tanvir想从节点1的位置走到节点n的位置, 输出最短距离, ...

  2. Lightoj 1016 - Brush (II)

    After the long contest, Samee returned home and got angry after seeing his room dusty. Who likes to ...

  3. lightoj 1019

    裸的最短路 dijkstra #include<cstdio> #include<string> #include<cstring> #include<ios ...

  4. lightOJ 1017 Brush (III) DP

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1017 搞了一个下午才弄出来,,,,, 还是线性DP做的不够啊 看过数据量就知道 ...

  5. LightOJ 1017 - Brush (III) 记忆化搜索+细节

    http://www.lightoj.com/volume_showproblem.php?problem=1017 题意:给出刷子的宽和最多横扫次数,问被扫除最多的点是多少个. 思路:状态设计DP[ ...

  6. [LightOJ 1018]Brush (IV)[状压DP]

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1018 题意分析:平面上有不超过N个点,如今能够随意方向划直线将它们划去,问:最少要划 ...

  7. Lightoj 1018 - Brush (IV)

    1018 - Brush (IV)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Muba ...

  8. Lightoj 1017 - Brush (III)

    1017 - Brush (III)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Sam ...

  9. light oj 1019【最短路模板】

    1019 - Brush (V) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Tanvir r ...

随机推荐

  1. robot framework常见错误:RIDE运行一次后不显示log

    在使用RIDE进行web自动化测试时,会发现经常运行第二遍不显示下方log,如下 原因: 代码使用的是谷歌浏览器.IE浏览器测试,运行一次后chromedriver.exe,IEDriverServe ...

  2. Pychorm提示Unresolved reference 导入模块报错

    最近使用Pychorm编写Python时,每次要引入自定义模块,就会报错,提示“Unresolved reference” Unresolved reference 'LoginClass' more ...

  3. 尼姆博弈扩展形式(一): 限定每次取物的上限。NYOJ-135,难度5~~~

    取石子(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 http://acm.nyist.net/JudgeOnline/problem.php?pid=135 描述 小 ...

  4. ZOJ 3811 Untrusted Patrol【并查集】

    题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发, ...

  5. 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling

    题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes onl ...

  6. hdu5608:function

    $n^2-3n+2=\sum_{d|i}f(i)$,问$f(i)$前$n$项和. 方法一:直接切入! $S(n)=\sum_{i=1}^{n}f(i)=\sum_{i=1}^{n}(i^2-3i+2- ...

  7. hdu3853:LOOPS

    题目大意:r*c个点,每个点有Aij的概率回到自己本身,Bij的概率向右一格,Cij的概率向下一格,求从(1,1)到(r,c)的期望步数. 题解:有了hdu4405的经验,从后往前推期望.那么,E(i ...

  8. hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]

    传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...

  9. OC温习四:数组

    /** arrayByAddingObject -- 往数组A添加一个数据,返回一个数组的形式,即必须有一个数组来接受 */ NSArray *array = [NSArray arrayWithOb ...

  10. POJ 2135 最小费用最大流 入门题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7441 Descri ...