Dijk入门(杭电2544题)

#include<iostream>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int n,m;
int map[105][105];
int vis[105];
int stemp[105];
int dijk(){
    memset(vis,0,sizeof(vis));
    vis[1]=1; //标记第一个已选
    memset(stemp,0,sizeof(stemp));
    int min;
    int flag1=100000,countx=0,county,index2;
    while(county!=n){//如果是联通图那最先遍历到的就是最小的
        for(int i=1;i<=n;i++){//第一重循环寻找可以扩展的点
            if(vis[i]){
                min=INF;
                for(int j=1;j<=n;j++){//第二重循环找到该点最短的扩展点
                    if(!vis[j]&&min>map[i][j]){
                        index2=j;
                        min=map[i][j];
                    }
                }
             }
             if(vis[i]&&min+stemp[i]<stemp[countx]+flag1){ //比较每个点的最小扩展点,选出总距离最短的标记该点和对应的扩展点
                flag1=min;
                countx=i;
                county=index2;
             }
        }
//        cout<<"!"<< flag1<<‘ ‘<<countx<<‘ ‘<<county<<endl;
        map[county][countx]=INF;//一但往前走就不存在回溯
        stemp[county]=flag1+stemp[countx];//记录每一个已选的路程
        vis[county]=1;
        flag1=INF;//初始化第一个点满足条件
        countx=0;
    }
    return stemp[n];
}
int main(){
    while(cin>>n>>m){
        if(!n&&!m) break;
        memset(map,INF,sizeof(map));
        while(m--){
            int a,b,c;
            cin>>a>>b>>c;
            map[a][b]=c;
            map[b][a]=c;
        }
    cout<<dijk()<<endl;
    }
}

原文地址:https://www.cnblogs.com/yzbpxx/p/10841676.html

时间: 2024-11-09 06:06:37

Dijk入门(杭电2544题)的相关文章

acm入门 杭电1001题 有关溢出的考虑

最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The input will consist of a series of integers n, one integer per line Output For each case, output SUM(n) in one line

杭电 2544 最短路

http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30857    Accepted Submission(s): 13293 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.

杭电dp题集,附链接

Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率j之下能抢的大洋); 正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v)  其中,f[j]表示抢j块大洋的最大的逃脱概率,条件是f[j-q[i].money]可达,也就是

杭电水题

杭电OJ上的水题:C语言程序设计练习 题目如下: 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2001 注:pid=后面可以为2000 - 2050中的任何一个(对应50道题目) 另外也可以做1000之后的(按顺序做,从1000开始把,1000开头的都蛮简单的) 原文地址:https://www.cnblogs.com/wyb666/p/10909202.html

小试牛刀-搜索基础入门(杭电五题)

hdu 1241 Oil Deposits 水题,BFS,判断区域的块数. 代码清单: #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef pair<int,int>P; int m,n; char s[105][105]; int xy[8][2]=

杭电算法题 HDU 1000-1004

h1,h2,h3,h4,h5,h6,p,blockquote { margin: 0; padding: 0 } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif; font-size: 13px; line-height: 18px; color: #737373; background-color: white; margin: 10px

动态规划解决杭电OJ1080题——LCS的变种

首先上题目: Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2570    Accepted Submission(s): 1451 Problem Description It is well known that a human gene can be considered as a se

杭电一水题(DFS)Untitled

Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from b1,…,bn in any order, say c1,…,cr, we want to make sure that a mod c1 mod c2 mod… mod cr=0 (i.e., a will become the remainder divided by ci each time,

杭电二分题

Description Now, here is a fuction: F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<=T<=100) which means the number of te