hdu 4738 桥
题目:还是自己看题目吧
trick:当不连通时不需要人去炸。否则,当桥的费用为0时当然需要一个人去炸。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <stack> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; #define debug puts("here") #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define REP(i,a,b) for(int i=a;i<=b;i++) #define foreach(i,vec) for(unsigned i=0;i<vec.size();i++) #define pb push_back #define RD(n) scanf("%d",&n) #define RD2(x,y) scanf("%d%d",&x,&y) #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w) #define All(vec) vec.begin(),vec.end() #define MP make_pair #define PII pair<int,int> #define PQ priority_queue #define cmax(x,y) x = max(x,y) #define cmin(x,y) x = min(x,y) #define Clear(x) memset(x,0,sizeof(x)) /* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB char *p = (char*)malloc(size) + size; __asm__("movl %0, %%esp\n" :: "r"(p) ); */ char IN; bool NEG; inline void Int( int &x){ NEG = 0; while (! isdigit (IN= getchar ())) if (IN== '-' )NEG = 1; x = IN- '0' ; while ( isdigit (IN= getchar ())) x = x*10+IN- '0' ; if (NEG)x = -x; } inline void LL(ll &x){ NEG = 0; while (! isdigit (IN= getchar ())) if (IN== '-' )NEG = 1; x = IN- '0' ; while ( isdigit (IN= getchar ())) x = x*10+IN- '0' ; if (NEG)x = -x; } /******** program ********************/ const int MAXN = 1005; const int INF = 1e9; int low[MAXN],dfn[MAXN],dep; int po[MAXN],tol; int best; struct Edge{ int y,cost,id,next; }edge[MAXN*MAXN<<1]; void add( int x, int y, int cost, int id){ edge[++tol].y = y; edge[tol].cost = cost; edge[tol].id = id; edge[tol].next = po[x]; po[x] = tol; } void dfs( int x, int fid){ dfn[x] = low[x] = ++ dep; for ( int i=po[x];i;i=edge[i].next){ int y = edge[i].y; int id = edge[i].id; if (id==fid) continue ; if (!dfn[y]){ dfs(y,id); cmin(low[x],low[y]); if (low[y]>dfn[x]){ if (edge[i].cost<best) best = edge[i].cost; } } else cmin(low[x],dfn[y]); } } int main(){ #ifndef ONLINE_JUDGE freopen ( "sum.in" , "r" ,stdin); //freopen("sum.out","w",stdout); #endif int size = 256 << 20; // 256MB char *p = ( char *) malloc (size) + size; __asm__( "movl %0, %%esp\n" :: "r" (p) ); int n,m,x,y,cost; while (RD2(n,m),n||m){ Clear(po); tol = 0; rep1(i,m){ RD3(x,y,cost); if (x>n||y>n) continue ; add(x,y,cost,i); add(y,x,cost,i); } dep = 0; Clear(dfn); best = INF; int ok = 0; rep1(i,n) if (!dfn[i]){ ok ++; dfs(i,0); } if (best==0)best = 1; if (ok>1)best = 0; printf ( "%d\n" ,best==INF?-1:best); } return 0; } |
分类:
图算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决